【发布时间】:2012-08-18 17:41:14
【问题描述】:
如果将十进制 (例如 49.9) 发送到 next 变量,则下面的代码会显示错误。你能告诉我为什么吗?为什么int()会转成整数?
next=raw_input("> ")
how_much = int(next)
if how_much < 50:
print"Nice, you're not greedy, you win"
exit(0)
else:
dead("You greedy bastard!")
如果我不使用 int() 或 float() 而只是使用:
how_much=next
即使我输入49.8,它也会移动到“else”。
【问题讨论】:
-
你可能想说
how_much = int(float(...))或how_much = int(round(float(...)))。 -
小注:
next不是 var 名称的好选择,因为自 python 2.6 以来就有一个内置函数next() -
你在这里是因为这个。 learnpythonthehardway.org/book/ex35.html