Python If-elif语句

时间:2015-08-09 07:31:39

标签: python if-statement

快速提问,我正在开发一个程序,该程序从用户那里获取一个数字并使用算法来试图击败他们的分数。无论如何,当他们从列表中选择一个数字时,它应该将它存储到humanint变量中,但是当我做elif时终端只是关闭数字的输入。任何想法?

numbers = 16, 9, 12, -14, -9, -10
print (numbers)
humanint = input("What number will you place your piece on?")
print ("Player 1 (Black) selects " + humanint + "!")
print("(*) Denotes terminal value (sum)")

if (humanint == 16):
    mmtree = '[{A:16} [{B:9} [{*G:25}]] [{C:12} [{*H:28}]] [{D:-14} [{*I:2}]] [{E:-9} [{*J:7}]] [{F:9} [{*K:6}]] ]'
elif (humanint == 9):
    mmtree = '[{A:9} [{B:16} [{*G:25}]] [{C:12} [{*H:21}]] [{D:-14} [{*I:-5}]] [{E:-9} [{*J:0}]] [{F:-10} [{*K:-1}]] ]'
elif (humanint == 12):
    mmtree = '[{A:12} [{B:16} [{*G:28}]] [{C:9} [{*H:21}]] [{D:-14} [{*I:-2}]] [{E:-9} [{*J:3}]] [{F:-10} [{*K:2}]] ]'
elif (humanint == -14):
    mmtree = '[{A:-14} [{B:16} [{*G:2}]] [{C:9} [{*H:-5}]] [{D:12} [{*I:-2}]] [{E:-9} [{*J:-23}]] [{F:-10} [{*K:-24}]] ]'
elif (humanint == -9):
    mmtree = '[{A:-9} [{B:16} [{*G:7}]] [{C:9} [{*H:0}]] [{D:12} [{*I:3}]] [{E:-14} [{*J:-23}]] [{F:-10} [{*K:-19}]] ]'
elif (humanint == -10):
    mmtree = '[{A:-10} [{B:16} [{*G:6}]] [{C:9} [{*H:1}]] [{D:12} [{*I:2}]] [{E:-14} [{*J:-24}]] [{F:-9} [{*K:-19}]] ]'

2 个答案:

答案 0 :(得分:2)

函数raw_input将始终返回字符串而不是整数。你需要像这样对用户输入进行类型转换,

humanint = int(input("What number will you place your piece on?"))

更新:为了找到问题,我尝试在我的机器上运行代码。事实证明,还有一个问题是你在打印内部连接字符串。以下似乎适用于Python 2.7

numbers = 16, 9, 12, -14, -9, -10
print (numbers)
humanint = input("What number will you place your piece on?")
print ("Player 1 (Black) selects " + str(humanint) + "!")
print("(*) Denotes terminal value (sum)")

if (humanint == 16):
    mmtree = '[{A:16} [{B:9} [{*G:25}]] [{C:12} [{*H:28}]] [{D:-14}[{*I:2}]][{E:-9} [{*J:7}]] [{F:9} [{*K:6}]] ]'
elif (humanint == 9):
    mmtree = '[{A:9} [{B:16} [{*G:25}]] [{C:12} [{*H:21}]] [{D:-14} [{*I:-5}]] [{E:-9} [{*J:0}]] [{F:-10} [{*K:-1}]] ]'
elif (humanint == 12):
    mmtree = '[{A:12} [{B:16} [{*G:28}]] [{C:9} [{*H:21}]] [{D:-14} [{*I:-2}]] [{E:-9} [{*J:3}]] [{F:-10} [{*K:2}]] ]'
elif (humanint == -14):
    mmtree = '[{A:-14} [{B:16} [{*G:2}]] [{C:9} [{*H:-5}]] [{D:12} [{*I:-2}]] [{E:-9} [{*J:-23}]] [{F:-10} [{*K:-24}]] ]'
elif (humanint == -9):
    mmtree = '[{A:-9} [{B:16} [{*G:7}]] [{C:9} [{*H:0}]] [{D:12} [{*I:3}]] [{E:-14} [{*J:-23}]] [{F:-10} [{*K:-19}]] ]'
elif (humanint == -10):
    mmtree = '[{A:-10} [{B:16} [{*G:6}]] [{C:9} [{*H:1}]] [{D:12} [{*I:2}]] [{E:-14} [{*J:-24}]] [{F:-9} [{*K:-19}]] ]'

print mmtree

答案 1 :(得分:2)

不要离题,但我认为使用这么多的elif不是pythonic。你应该使用dict。

options = {
16: '[{A:16} [{B:9} [{*G:25}]] [{C:12} [{*H:28}]] [{D:-14} [{*I:2}]] [{E:-9} [{*J:7}]] [{F:9} [{*K:6}]] ]',
9: '[{A:9} [{B:16} [{*G:25}]] [{C:12} [{*H:21}]] [{D:-14} [{*I:-5}]] [{E:-9} [{*J:0}]] [{F:-10} [{*K:-1}]] ]',
12: '[{A:12} [{B:16} [{*G:28}]] [{C:9} [{*H:21}]] [{D:-14} [{*I:-2}]] [{E:-9} [{*J:3}]] [{F:-10} [{*K:2}]] ]',
-14: '[{A:-14} [{B:16} [{*G:2}]] [{C:9} [{*H:-5}]] [{D:12} [{*I:-2}]] [{E:-9} [{*J:-23}]] [{F:-10} [{*K:-24}]] ]',
9: '[{A:-9} [{B:16} [{*G:7}]] [{C:9} [{*H:0}]] [{D:12} [{*I:3}]] [{E:-14} [{*J:-23}]] [{F:-10} [{*K:-19}]] ]',
-10: '[{A:-10} [{B:16} [{*G:6}]] [{C:9} [{*H:1}]] [{D:12} [{*I:2}]] [{E:-14} [{*J:-24}]] [{F:-9} [{*K:-19}]] ]'
}


humanint = int(input("What number will you place your piece on?"))
mmtree = options.get(humanint)