简体   繁体   中英

Why does not `input` show up on python?

I am making simple game which there are 2 players and 20 sticks given. Each player can pick 1-3 stick(s) and player picks last stick will lose the game.

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])

So, function stix shows 20 sticks and that's it. It does not ask please pick stick(s) up to 3 . What did I miss in here?

*I am using python 2.6

Thanks in advance!

You're never entering the while loop at all:

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

So the error, in this case, has nothing to do with input()

Your while case is that "game_over" does not equal 0 but the line directly above it sets it to 0.

I think what you want is:

game_over = True
while not game_over:
   ...

Your problem is that your while loop is going to run while game_over is not 0, but in the line before, you set game_over to 0.

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

Thus, change game_over to 1, and your code will work!

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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