学习Python困难法练习35求助
不知道为什么,当游戏到达金色房间时,它就不正常了。我输入任何数字都会收到死亡提示:“伙计,学着输入一个数字。”
谢谢
from sys import exit
def gold_room():
print 'this room is full of gold, how much do you take?'
next = raw_input('> ')
if '0' in next or '1' in next:
how_much = int(next)
else:
dead('man, learn how to type a number')
if how_much < 50:
print 'nice! your not too greedy. you win!'
exit(0)
else:
dead('you greedy bastard!')
def bear_room():
print 'there is a bear here.'
print 'the bear has a bunch of honey'
print 'the fat bear is in fromt of another door'
print 'how are you going to move the bear?'
bear_moved = False
while True:
next = raw_input('> ')
if next == 'take honey':
dead('the bear looks at you then pimp slaps you in the face')
elif next == 'taunt bear' and not bear_moved:
print 'the bear has moved from the door now. you can go through.'
bear_moved = True
elif next == 'taunt bear' and bear_moved:
dead('the bear gets pissed off and chews your crotch off')
elif next == 'open door' and bear_moved:
gold_room()
else:
print 'i have no idea what that means.'
def bolofumps_room():
print 'here you see the great evil bolofump'
print 'he, it whatever stares at you and you go insane'
print 'do you flee for your life or eat your head?'
next = raw_input('> ')
if 'flee' in next:
start()
elif 'head' in next:
dead('well, that was tasty!')
else:
bolofumps_room()
def dead(why):
print why, 'good job!'
exit(0)
def start():
print 'you are in a dark room'
print 'there is a door to your left and right'
print 'which one do you take?'
next = raw_input('> ')
if next == 'left':
bear_room()
elif next == 'right':
bolofumps_room()
else:
dead('you stumble around the room until you starve to death')
start()
补充:输入1可以,但输入2就不行。
8 个回答
1
我可能漏掉了一些东西,但我就是这样改的。代码更少,运行得很好。
Def gold_room():
Print("This room is full of gold. How much do you take?")
choice = input(' ')
next = int(choice)
If next > 50:
dead("Man, learn to type a number.")
else:
print("your not greedy. Good job!")
exit(0)
1
在这个教程中,你需要了解的内容包括:
- 解析参数
- 读取用户输入
- 使用条件语句和循环,比如 if、loop 和 while
- 函数的使用
- 打印输出
- 列表及其特定功能
在这个阶段,你还不能使用一些高级功能,比如捕捉错误或者使用 "isdigit()" 这个函数。
通过尝试其他例子,我发现对字符串使用 "sorted" 函数时,会把所有字符分开,我会在这里用到这个方法。
我对数字的定义是:“只包含 0 到 9 的字符的字符串”。这个定义对于这个练习来说已经足够了。
所以我的做法是从输入中去掉所有数字,然后检查剩下的内容是否为空。如果是空的,那就是一个整数;如果还有其他字符,那就不是。
def remove_all_occurences(char_list, item):
value = "%d" % item # convert to string
while char_list.count(value) != 0:
char_list.remove(value)
return char_list
def is_int(input):
"""Personnal function to test if something is an int"""
# The sort separates all the characters of the list
characters_list = sorted(input)
for num in range(0,10):
characters_list = remove_all_occurences(characters_list, num)
return len(characters_list) == 0
9
在 gold_room
里,你这样做:
next = raw_input('> ')
if '0' in next or '1' in next:
how_much = int(next)
else:
dead('man, learn how to type a number')
它只检查 '0' in next or '1' in next
,所以 '2' 不起作用也就不奇怪了,对吧?
你想要的应该是这样的:
next = raw_input('> ')
try:
how_much = int(next)
except ValueError:
dead('man, learn how to type a number')
其实也可以不使用异常来实现,但请记住,避免使用像异常这样重要和基础的东西是个很糟糕的主意。我希望书里能在后面讲清楚这一点。
总之,我们知道 int
只接受数字,所以我们只需要检查这一点:
if next.isdigit():
how_much = int(next)