为什么`input`不显示在python上?

时间:2016-04-18 02:25:27

标签: python

我正在制作简单的游戏,其中有2名玩家和20支。每个玩家可以选择1-3支球员和球员选秀权最后一根球棒将输掉比赛。

def stix(num):
    for _ in range(5): print('|  '* num)
    print
stix(20)
game_over = 0
while game_over !=0:
    players={}
    for i in range(2):
        players[i] = int(input('Player %d: Please pick stick(s) up to 3' %i))
        if players > 3 or players<0:
            print('Please pick between 1 - 3 stick(s)')
        else:
            stix-=players
            if stix <= 0:
                print('Player[i] lost')
                break
            else:
                print('There is %d stick(s) left' %stix)
                print(stix-players[i])

所以,函数stix显示20支,就是这样。它没有问please pick stick(s) up to 3。我在这里想念的是什么?

*我正在使用python 2.6

提前致谢!

3 个答案:

答案 0 :(得分:3)

你永远不会进入while循环:

game_over = 0
while game_over !=0: # Evaluated to false the first time so it's skipped.
    # code

因此,在这种情况下,错误与input()

无关

答案 1 :(得分:0)

你的情况是“game_over”不等于0但直接在它上面的行将它设置为0.

我认为你想要的是:

game_over = True
while not game_over:
   ...

答案 2 :(得分:0)

你的问题是你的while循环会在game_over 0时运行,但在之前的行中,你将game_over设置为0。

game_over = 0
while game_over !=0:
    ...

因此,将game_over更改为1,您的代码就可以使用了!

def stix(num):
    for _ in range(5): print('|  '* num)
    print
stix(20)
game_over = 1
while game_over !=0:
    players={}
    for i in range(2):
        players[i] = int(input('Player %d: Please pick stick(s) up to 3' %i))
        if players > 3 or players<0:
            print('Please pick between 1 - 3 stick(s)')
        else:
            stix-=players
            if stix <= 0:
                print('Player[i] lost')
                break
            else:
                print('There is %d stick(s) left' %stix)
                print(stix-players[i])