python将字符串转换为带有int()的整数,语法错误,找到多个语句

时间:2013-10-15 04:19:21

标签: python int

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!")

3 个答案:

答案 0 :(得分: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,对我来说似乎没问题。我不能重现它,因为你刚才说你有相同的语法。

那条橙色线是什么?复制并粘贴完全相同的代码。尝试一下。

答案 1 :(得分: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未定义。

答案 2 :(得分:0)

您的屏幕播放显示错误,因为converted_age未定义。

您有convertedAge = int(age)并且您正在尝试比较未定义的converted_age