当前位置: 首页 > 面试题库 >

学习Python的艰辛方法练习35帮助

东郭存
2023-03-14
问题内容

由于某种原因,当游戏到达金牌室时,它将无法正常工作。当我输入任何数字时,我会收到死亡消息“男人,学会键入数字”

谢谢

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()

编辑:键入一个有效,但2不起作用


问题答案:

您在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)


 类似资料:
  • 问题内容: http://learnpythonthehardway.org/book/ex6.html Zed在这里似乎可以互换使用,两者之间有什么区别吗?为什么不一直使用呢? 另外,我不确定要在文档中搜索什么以找到关于此的更多信息。什么是和究竟叫什么名字?格式化字符串? 问题答案: 它们称为字符串格式化操作。 %s和%r之间的区别在于%s使用函数,而%r使用函数。你可以阅读有关之间的差异,并在

  • 问题内容: 我试图理解下面的代码,并且遇到了困难。有人可以回答几个问题来帮助我理解吗? 我知道PHRASES(变量)包含字典,因此“类%%%(%%%)”映射为“使名为%%%的类为-a %%%”。 这些行(第2-13行)是否也实际上是在创建类,还是就像“字符串”一样?因为它看起来可能正在创建类,但是我不确定。 我也知道%%%, * 和@@@实际上会被代码中某些地方的实际单词替换,但是不知道如何或在哪

  • 解析中的最后一个练习应该既具有挑战性又有趣。你终于可以看到,你的微型 Python 脚本运行并做了一些事情。难以理解这个章节和解析的概念很正常。如果你发现你已经到达了这里,而且你不太明白发生了什么,请退后一步,再考虑在这一部分做一些练习。在继续之前,重复几次这个章节,这可以帮助你在最后两个练习中制作自己的小语言。 我故意不会在本练习中包含任何代码,以便你必须根据解释器工作方式的描述来尝试。你已经有

  • 这个练习中我打算涉及到四个排序算法和一个搜索算法。排序算法是快速排序、堆排序、归并排序和基数排序。之后在你完成基数排序之后,我打算想你展示二分搜索。 然而,我是一个懒人,大多数C标准库都实现了堆排序、快速排序和归并排序算法,你可以直接使用它们: #include <lcthw/darray_algos.h> #include <stdlib.h> int DArray_qsort(DArray

  • 问题内容: 我正在执行Zed Shaw的精彩学习Python The Hard Way ,但我遇到了一个额外的问题:9– 10行可以写成一行,怎么写?我尝试了一些不同的想法,但无济于事。我可以继续前进,但是那会带来什么乐趣呢? Zed还写道,他可以在一行中完成整个脚本。我不确定他的意思。 随时随地为我提供帮助:给出答案或仅作提示-甚至可能包含对该问题的模糊或隐藏答案。 问题答案:

  • 陈懂没说话,指指绝影的餐盘示意他继续吃,绝影这才又动起手来,于是陈董也接着凝重地说:“五年了阿。公司发展都今天真不容易,说实话,这其中还是多亏了你。人家说公司三年是个槛,就三年,能上去就上去,上不去就关门。现在想想,在三年的时候,公司还真是靠着你才撑下去。” 陈懂这么一说,绝影有点动容,突然之间,脑子里涌现出的全是以前在公司的事情。从第一天到公司做的面试题,到BOSS Liu,到张厂长,到Bug