小编Pea*_*key的帖子

为什么Python函数不打印默认值?

我是Python新手,我正在尝试打印参数的默认值集.我的代码如下;

# Functions

def shoppingcart(item='computer', *price):
    print item
    for i in price:
        print i

shoppingcart(100,200,300)
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我得到的只是值100,200,300.我在期待computer,100,200,300.我究竟做错了什么?

编辑

我如何在Python 2.7中实现这一目标?

编辑

我将我的代码更新为

def shoppingcart(item='computer', *price):
    print item
    for i in price:
        print i

shoppingcart(item='laptop', 100,200,300)
Run Code Online (Sandbox Code Playgroud)

但得到错误

在此输入图像描述

python python-2.7

2
推荐指数
1
解决办法
183
查看次数

为什么这样!isset似乎不起作用?

我是PHP的新手,并且已经整合了一个将输入值相乘的表单.但是,当我尝试验证某人是否未输入任何值以返回错误消息时,它会显示该消息.我的代码如下.感谢您是否也可以提出改进建议.

<?php

$counter = 0;

if(isset($_POST["submit"])) {
    $start = $_POST["start"];
    $end = $_POST["end"];
    $multiply = $_POST["multiplication"];

// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
    // print "Please enter some values";
// }

if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
    print "Please enter some values";

}

// for($start;$start<$end;$start++) {
    // $counter = $counter +1;
    // $multiplication = $counter * $multiply;
    // print "$counter <br />";
    // print "$counter multiplied by $multiply = $multiplication <br />";

// }

}

?> …
Run Code Online (Sandbox Code Playgroud)

php forms validation isset

1
推荐指数
1
解决办法
3295
查看次数

PHP返回循环结果

我是编码和PHP的新手,我想知道如何return在循环时使用它.例如,我想返回/显示1-10但不使用echo.

$start = 1;
$end = 11;

for($start; $start < $end; $start=$start+1) {

echo $start; //how can I use return?

}
Run Code Online (Sandbox Code Playgroud)

php loops return

1
推荐指数
1
解决办法
1万
查看次数

有没有办法让我的PHP代码被审查?

我是编码和PHP的新手.当我正在为小客户端组建一个小型Web应用程序时,如何在不泄露客户认为对他们私有的内容的情况下检查我的代码,以便我知道我的代码编写得很好,符合标准和安全吗?

php

1
推荐指数
1
解决办法
121
查看次数

在SQL Server 2008中运行查询时是否需要包含方括号[]?

我安装了SQL Server 2008 Express Edition,这是我第一次运行SQL查询.我注意到,如果我选择显示前1000行,查询将括号[]放在数据库名称及其各自的列周围.

  1. 为什么数据库名称和列周围有括号?
  2. 有没有需要这些,如果是这样的话?

sql sql-server-2008

1
推荐指数
1
解决办法
2657
查看次数

Internet Explorer 7+返回错误"Object expected",尽管其他浏览器都没有

每次我在Internet Explorer 7或更高版本中加载页面时,我Object Expected在调用下面的函数时都会收到错误.该脚本显示在结束</body>标记之前的页面底部.没有相同的元素nameid.

<script type="text/javascript">
     window.onload = show();
</script>
Run Code Online (Sandbox Code Playgroud)

它试图调用的Javascript函数是

function show() {
    obj1
     = document.getElementById("container").innerHTML
     = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
         + '<p><a href="#" onclick="hide();">test</a></p></div>';
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么此错误不会出现在任何其他浏览器中?
  2. 我需要更改什么才能解决问题?

编辑0

如果我将函数移动show到同一个块中window.onload,hide()现在不再有效.

Javascript代码

function show() {
    obj1
     = document.getElementById("container").innerHTML
     = '<div style="width: 960px; height: 2000px;"><p>Hello World.<br>Here I am.</p>'
         + '<p><a href="#" onclick="hide();">test</a></p></div>';
}

function hide() {   
    obj1 = document.getElementById("container");
    if(obj1){
        alert("Hi");
        obj1.style.display = …
Run Code Online (Sandbox Code Playgroud)

javascript internet-explorer internet-explorer-7

1
推荐指数
1
解决办法
2958
查看次数

为什么Javascript中的参数在使用console.log时返回[object Arguments]?

我有以下功能

//simple function with parameters and variable
        function thirdfunction(a,b,c,d){
            console.log("the value of a is: " + a);
            console.log("the value of b is: " + b);
            console.log("the value of c is: " + c);
            console.log("the value of d is: " + d);
            console.log("the arguments for each values are: " + arguments);
            console.log("the number of arguments passed are: " + arguments.length);
        }

        console.log("no parameter values are passed");
        thirdfunction();

        console.log("only a and b parameter values are passed");
        thirdfunction(1,2);
Run Code Online (Sandbox Code Playgroud)

但是,arguments如果我连接文本,则不会显示传入的值the arguments …

javascript

1
推荐指数
1
解决办法
770
查看次数

如何在javascript中执行循环?

请原谅我的n00bness,因为我是Javascript和循环世界的新手,但我不知道如何最好地for loops在javascript中接近.希望理解for loops将有助于揭示其他类型的循环.

示例代码

for (var i=0; i < 10; i=i+1) {
    document.write("This is number " + i);
}
Run Code Online (Sandbox Code Playgroud)

我的理解是,当i初始化时,它从值0开始,然后根据条件进行评估< 10.如果它小于10,它执行该语句document.write("This is number + i);一旦它执行上述语句,才把它递增1的下一个值.

我咨询过的指南;

  1. http://www.functionx.com/javascript/Lesson11.htm
  2. http://www.cs.brown.edu/courses/bridge/1998/res/javascript/javascript-tutorial.html#10.1
  3. http://www.tizag.com/javascriptT/javascriptfor.php

现在,http: //www.functionx.com/javascript/Lesson11.htm上的指南似乎另有说明了

要执行此循环,将检查"启动"条件.这通常是计数应该开始的初始值.接下来,测试条件; 此测试确定循环是否应继续.如果测试呈现真实结果,则表达式用于修改循环并执行Statement.执行Statement后,循环重新开始.

抛出我的那条线是If the test renders a true result, then the Expression is used to modify the loop and the Statement is executed.似乎推断因为0小于10,所以修改增量表达式,其将是0 + 1,然后document.write执行该语句.

我的问题

解释for循环的正确方法是什么?我自己的理解是否正确?同样的理解是否适用于其他编程语言,例如PHP,Perl,Python等?

javascript for-loop

1
推荐指数
2
解决办法
2659
查看次数

我在Javascript中使用以下try,throw和catch异常处理程序做错了什么?

我有以下代码,但不使用throw,但是当我使用throw关键字时,它不会返回相应的消息.我究竟做错了什么?

更新

我故意调用该函数addme,而不是addMe因为我想捕获错误消息.

代码 - 无需使用即可使用 throw

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
    }


    catch (err) {
        document.write(err.name + " " + err.message);
    }
Run Code Online (Sandbox Code Playgroud)

代码 - 这不起作用

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
        throw "error 1";
    }

    catch(err) {
        if(err ==  "error 1") {
            document.write("This is a custom message for error 1");
        }
    }
Run Code Online (Sandbox Code Playgroud)

javascript

1
推荐指数
1
解决办法
96
查看次数

PHP strtotime函数quandry

我是编码和PHP的新手,并且对下面代码中的内容感到困惑'-01'.

strtotime($someYear . '-' . $someMonth . '-01');
Run Code Online (Sandbox Code Playgroud)

php date

0
推荐指数
1
解决办法
70
查看次数