学习python的艰苦训练35帮助

2024-05-15 00:55:31 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某种原因,当游戏到达黄金屋时,它就不能正常工作。当我输入任何数字时,我会收到死亡信息“伙计,学着输入数字”

谢谢

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个无效


Tags: andtheinyouyourifdefelse
3条回答

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)

如果你考虑一下你现在在教程中应该知道的,包括

  • 分析参数
  • 读取用户输入
  • 使用if/loop/while
  • 功能
  • 打印
  • 列表及其特定功能

不能使用诸如捕捉错误和或使用“isdigit()”之类的神奇函数。

在另一个例子中,我发现在字符串上使用“sorted”,可以分隔所有字符,我将在这里使用它。

我想,我对数字的定义是“只包含0到9个字符的字符串”。这足够做运动了。

所以我的方法是删除输入中的所有数字,并检查它是否以空结尾。如果是这样,它是一个int,否则如果还有字符,它就不是。

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

我可能遗漏了什么,但这就是我改变它的方式。代码更少,运行良好。

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)

相关问题 更多 >

    热门问题