-4

m doing a very simple piece of code, and it keeps generating a Syntax error, multiple statements found while compiling a single statement. I can看不出我的代码有什么问题!:( ,请查看链接,因为我在这里输入的代码可能是正确的,但我看不出它与 IDLE 链接中的代码之间的区别。

我正在尝试将字符串 '10' 转换为整数,以便 if 语句起作用。

这是屏幕截图。 http://www.screencast.com/t/0zItqcn5P6d

age = '10'
converted_age = int(age)
if converted_age == 10:
    print("whats the best way to speak to a monster?")
    print("from as far away as possible!")
4

3 回答 3

1

你错过了:在你的 if 声明之后。

好吧,我刚刚执行了

age = '10'
converted_age = int(age)
if converted_age == 10:
    print("whats the best way to speak to a monster?")
    print("from as far away as possible!")

使用 python2.7 和 3.0 对我来说似乎没问题。我无法重现它,因为你刚才说你有上面相同的语法。

那条橙色线是什么?复制并粘贴与上面完全相同的代码。试试看。

于 2013-10-15T04:21:24.153 回答
1
age = '10'
converted_age = int(age)
if converted_age == 10:
                      ^ Colon needed here.
    print("whats the best way to speak to a monster?")
    print("from as far away as possible!")

控制台会话:

>>> age = '10'
converted_age = int(age)
if converted_age == 10:
    print("whats the best way to speak to a monster?")
    print("from as far away as possible!")
whats the best way to speak to a monster?
from as far away as possible!

在您的屏幕截图中,您使用ConvertedAge存储变量但与 进行比较converted_age,这会给出covnerted_age未定义的错误。

于 2013-10-15T04:22:10.247 回答
0

您的屏幕投射显示错误,因为 convert_age 未定义。

你有convertedAge = int(age)并且你正在尝试比较 undefined converted_age

于 2013-10-15T04:25:55.327 回答