参考此代码: http://learnpythonthehardway.org/book/ex35.html
第12行。
在黄金房间你输入49 python条件不会工作?! (它跳到其他地方) 这是python中的一个错误吗?!?! (顺便说一下,我使用的是2.7.8版本)
这是实际情况:
PS C:\Users\Sofia\python_projects> python.exe .\project.py
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. You can go through it now.
> open door
This room is full of gold. How much do you take?
> 49
**Man, learn to type a number**. Good job!
而49应该返回:
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
答案 0 :(得分:0)
你给我的文章很旧,并且谈论int()函数。
真正的问题与比较数字无关,而与他如何放置if和else条件的逻辑无关。
这是解决方案:
def gold_room():
print "This room is full of gold. How much do you take?"
choice = raw_input("> ")
how_much = int(choice)
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
elif how_much >=50:
dead("You greedy bastard!")
else:
dead("Man, learn to type a number.")
对不起伙计们(我只是一个初学者......)但是非常感谢你的帮助!