我一直在搞乱这段代码太久了,所以我想我会申请一些帮助...... 这对我来说根本不起作用。有谁知道我在做错了什么? 任何建议表示赞赏。
def gen_a(table):
for x in table: # same as for x in table.keys()
if x > 1000:
yield x
def gen_b(table):
for x in table: # same as for x in table.keys()
if x > x: # will never happen
yield x
table ={1249.99: 36.30,
1749.99: 54.50,
2249.99: 72.70,
2749.99: 90.80,
3249.99: 109.00,
3749.99: 127.20,
4249.99: 145.30}
x = 1000 # note that x isn't in the same scope as the other x's
print(next(gen_a(table))) # result varies since dict are unordered, I got 4249.99
print(next(gen_b(table))) # raises a StopIteration
答案 0 :(得分:3)
在你的"警告"在你的" addWrong"内打电话函数,你没有正确地进行字符串连接。
加号'绕过变量,在字符串引号之外:
alert(x + " + " + y + " = " + sum);
只是混淆了引号。
答案 1 :(得分:0)
你应该阅读字符串插值(使用字符串中的变量)。您的代码存在两个问题:
alert(x " + " y " = " + sum);
x和y是已定义的变量,正在与字符串一起进行评估,而没有任何类型的运算符表明您希望它们如何进行交互。这与键入x“r”y“z”相同,这也会出错。你可以把它写成:
alert(x + " + " + y + " = " + sum);
但阅读起来非常困惑。请参阅下面的建议。
另一个问题是如何从提示符(“X?”)和提示符(“Y?”)添加输入。
var x = prompt("X?");
无论用户回答什么,它都会被强制转换为字符串。当你将两个字符串一起添加时,'+'运算符用于连接字符串,而不是将它们一起添加(作为整数)。所以你需要将输入转换为整数。有更好的方法来解释边缘情况,但最简单的方法是在X和Y周围调用parseInt():
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bad Math (typeConv)</title>
</head>
<body>
<h1>Doing bad math... will she run??</h1>
<button type="button" onclick="addWrong()">addWrong</button>
<button type="button" onclick="sayHi()">sayHi</button>
<script>
function addWrong() {
var x = prompt("X?");
var y = prompt("Y?");
var sum = parseInt(x) + parseInt(y);
alert(`${x} + ${y} = ${sum}`);
} // end addWrong
function sayHi() {
alert("Hi");
}
</script>
</body>
</html>