为什么python上不显示“input”?

2024-05-23 15:57:19 发布

您现在位置: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

提前谢谢!你知道吗


Tags: ingamefor玩家rangenumoverstix
3条回答

你永远不会进入while循环:

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

因此,在本例中,错误与input()无关

while的情况是“game\u over”不等于0,但它正上方的一行将其设置为0。你知道吗

我想你想要的是:

game_over = True
while not game_over:
   ...

你的问题是你的while循环将在游戏结束时运行,而不是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])

相关问题 更多 >