[英]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.