Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python If elif语句_Python_If Statement - Fatal编程技术网

Python If elif语句

Python If elif语句,python,if-statement,Python,If Statement,快速提问,我正在开发一个程序,该程序从用户那里获取一个数字,并使用一个算法来试图击败他们的分数。无论如何,当他们从列表中选择一个数字时,它应该将其存储到humanint变量中,但是当我这样做时,如果终端只是在输入数字后关闭。有什么想法吗 numbers = 16, 9, 12, -14, -9, -10 print (numbers) humanint = input("What number will you place your piece on?") print ("Player 1 (B

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

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}]] ]'

函数raw_input将始终返回字符串而不是整数。您需要像这样键入用户输入

humanint = int(input("What number will you place your piece on?"))
更新:为了找到问题,我试着在我的机器上运行代码。结果发现还有一个问题,您在打印中连接了字符串。在Python2.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

函数raw_input将始终返回字符串而不是整数。您需要像这样键入用户输入

humanint = int(input("What number will you place your piece on?"))
更新:为了找到问题,我试着在我的机器上运行代码。结果发现还有一个问题,您在打印中连接了字符串。在Python2.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

我不想离题,但我认为使用这么多的elif's不是pythonic。你应该改用口述

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)

我不想离题,但我认为使用这么多的elif's不是pythonic。你应该改用口述

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)


你能详细说明你的问题吗?您的意思是,当您为humanint输入值时,如果命中elif部分,则不会得到任何输出?您是否得到if块的输出?您似乎没有在if..elif块中打印任何内容,您希望得到什么输出?请向我们描述您首先打开终端时遇到的错误,然后运行脚本。如果您只是运行脚本,它将打开终端,运行,然后在完成后立即关闭。当humanint首次初始化时,它允许我输入一个数字。初始化后,如果我试图从elif块mmtree返回或获取值,程序将崩溃。对原始输入进行类型转换不起作用。正如我所问的,请解释一下你所说的崩溃是什么意思。你有什么错误吗?或者只是没有输出?你还期待什么?你能详细说明你的问题吗?您的意思是,当您为humanint输入值时,如果命中elif部分,则不会得到任何输出?您是否得到if块的输出?您似乎没有在if..elif块中打印任何内容,您希望得到什么输出?请向我们描述您首先打开终端时遇到的错误,然后运行脚本。如果您只是运行脚本,它将打开终端,运行,然后在完成后立即关闭。当humanint首次初始化时,它允许我输入一个数字。初始化后,如果我试图从elif块mmtree返回或获取值,程序将崩溃。对原始输入进行类型转换不起作用。正如我所问的,请解释一下你所说的崩溃是什么意思。你有什么错误吗?或者只是没有输出?你还期待什么?我试着按照你的建议进行打字,但一旦程序点击elif块,仍然会崩溃。在你的评论中,我看到你说我希望根据用户输入的数字打印字符串。但是我没有看到任何打印声明。您应该在文件末尾添加print mmtree。这就是导致终端关闭的原因,试图以任何方式访问elif块。我试过打印mmtree。我试过像你建议的那样进行打字,但一旦程序点击elif块,仍然会崩溃。在你的评论中,我看到你说我希望根据用户输入的数字打印字符串。但是我没有看到任何打印声明。您应该在文件末尾添加print mmtree。这就是导致终端关闭的原因,试图以任何方式访问elif块。我试着打印出mmtree,很有效!非常感谢。对于无法识别的选项Python Zen,koan 10.get on dict返回None,而不是key error,可以很好地处理它!非常感谢。对于无法识别的选项Python Zen,koan 10.get on dict返回None,而不是key error,这可以很好地处理