简体   繁体   English

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

[英]Why does not `input` show up on python?

I am making simple game which there are 2 players and 20 sticks given. 我正在做一个简单的游戏,有2个玩家和20个棍棒。 Each player can pick 1-3 stick(s) and player picks last stick will lose the game. 每个玩家可以选择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])

So, function stix shows 20 sticks and that's it. 因此,功能stix显示20条,仅此而已。 It does not ask please pick stick(s) up to 3 . 它不要求please pick stick(s) up to 3 What did I miss in here? 我在这里想念什么?

*I am using python 2.6 *我正在使用python 2.6

Thanks in advance! 提前致谢!

You're never entering the while loop at all: 您根本不会进入while循环:

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() 因此,在这种情况下,错误与input()无关

Your while case is that "game_over" does not equal 0 but the line directly above it sets it to 0. 您的while情况是“ game_over”不等于0,而是直接位于其上方的行将其设置为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。

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

Thus, change game_over to 1, and your code will work! 因此,将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.

相关问题 为什么我的文件在python中显示为空白? - Why does my file show up blank in python? 为什么python变量在某些情况下不显示 - why does python variable not show up in certain situations 为什么我的字词在python中显示为undefined? - Why does my term show up as undefined in python? Python:为什么制表符在Tkinter中显得奇怪? - Python: Why does the tab character show up weird in Tkinter? 为什么python子模块没有显示在dir(module)的列表中? - why does python submodule not show up in list from dir(module)? 为什么代码没有显示错误? - Why does the code not show up with error? 为什么外键显示为下拉框? - Why does the foreignkey show up as a dropdown box? 为什么 python Kivy Boxlayout 在从最近的教程中 100% 复制代码时不显示? - why does python Kivy Boxlayout not show up when code is copied 100% from very recent tutorial? 当我分割一些HTML源代码时,为什么会出现b&#39;(有时是b&#39;&#39;)[Python] - why does b'(and sometimes b' ') show up when I split some HTML source[Python] 为什么通过子进程从“ git --version”读取的内容与“ python --version”显示在不同的频道上? - Why does reading from “git --version” show up on a different channel from “python --version” via subprocess?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM