繁体   English   中英

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

[英]Why does not `input` show up on 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

提前致谢!

您根本不会进入while循环:

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

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

您的while情况是“ game_over”不等于0,而是直接位于其上方的行将其设置为0。

我认为您想要的是:

game_over = True
while not game_over:
   ...

您的问题是当game_over 不为 0时,您的while循环将运行,但是在前面的一行中,您将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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM