'如果'语句未运行

时间:2014-11-25 20:05:07

标签: python if-statement dictionary

当我运行此代码时,出于某种奇怪的原因,if语句永远不会运行,因此控制台根本不返回任何内容。但是,代码是未完成的(因为您可能会看到其他3个选项未完成),但我编写了代码,以便我可以复制并粘贴其他选项。我试图添加一些print('test')来尝试找出问题;并且发现它始于if if skill == 'dexterity'语句。如果有人能提供帮助那就太棒了,因为我不知道这是我的计算机中的错误还是我的愚蠢。以前在这台计算机上已经发生过,而且它从未解决过。 编辑:我已将.lower更改为.lower()和while语句,但仍无济于事。 编辑2:我的IDLE被窃听,因此问题已经解决:D。

def charac_create():
player_skill = {
    'dexterity' : 0,
    'strength' : 0,
    'wisdom' : 0,
    'health' : 0
}
point_pool = 30
player_choice = input('Hello, and welcome to the skill tree!' \
                      ' What skill would you like to change:' \
                      ' Dexterity, Strength, Wisdom or Health?' \
                      ' You only have 30 points, so spend wisely!' \
                      ' If you want to exit, simply type "nope"!')
skill = player_choice.lower()
if skill == 'dexterity':
    print('test')
    return 'test'
    choice = input('Ahh, sleight of hand! A good choice.' \
                   'How would you like to change it?(+ or -)')
    while choice != '+' or choice != '-':
        print('That\'s not what I asked!')
        choice = input('How would you like to change it?(+ or -)')
    if choice == '+':
        n = int(input('By how much?'))
        if n < 0:
            print('This is a positive number, not negative!')
            n = int(input('By how much?'))
        elif n > 0:
            if n > pointpool:
                print('You don\'t have enough points!')
                n = int(input('By how much?'))
            elif n < pointpool: 
                pointpool -= n
                player_skill['dexterity'] += n
                print('Your dexterity is now ' + player_skill['dexterity'] + 'points,' \
                      'and you have ' + pointpool + ' points left!')
    elif choice == '-':
        n = int(input('By how much?'))
        if n > 0:
            print('This is a negative number, not positive!')
            n = int(input('By how much?'))
        elif n < 0:
            if player_skill['dexterity'] - n > 0:
                print('You can\'t take out more points than there are!')
                n = int(input('By how much?'))
            elif player_skill['dexterity'] - n < 0: 
                pointpool += n
                player_skill['dexterity'] -= n
                print('Your dexterity is now ' + player_skill['dexterity'] + 'points,' \
                      'and you have ' + pointpool + ' points left!')
else:
    print('test')
    return 'test'
charac_create()

4 个答案:

答案 0 :(得分:3)

此行将函数 lower分配给变量skill

skill = player_choice.lower

您需要调用函数将返回值赋给变量

skill = player_choice.lower()

请参阅以下示例

s = 'test'

>>> s.lower
<built-in method lower of str object at 0x01EC0960>  # this is the function

>>> s.lower()
'test'     # this is the returned value

答案 1 :(得分:0)

我尝试了你的代码,一切似乎都运行良好。 if语句下的代码正在执行,尽管我必须使用raw_input而不是input来使其工作。此外,我不得不在打印后删除返回,我猜你把它放在那里进行测试

答案 2 :(得分:0)

我已经将python IDLE重新安装到3.4.2并且设法让它再次运行;一定是一个错误的IDLE或我的电脑。感谢所有帮助我解决这个问题的人;你解决了很多语法错误! :d

答案 3 :(得分:0)

您需要使用while choice != '+' and choice !='-'代替或使您的逻辑工作。