Eclipse 为什么程序在执行前多次询问我输入?

Eclipse 为什么程序在执行前多次询问我输入?,eclipse,python-3.x,Eclipse,Python 3.x,我一直在做一个练习,让我对基础知识更加熟悉,但这个程序不会像我希望或期望的那样运行。 我已经使用输入等编写了类似的代码,但这是我第一次遇到这种情况,所以我想知道为什么这段代码没有按预期工作 我希望程序会问我“如果你同意,请键入“1”;如果你拒绝,请键入“0”。21次,根据键入的1或0,它会在变量“同意”或“拒绝”中添加一个,当然,在插入输入后会向我显示当前分数,如: Who wants to play volleyball on sunday from 4pm to 6pm? If more 1

我一直在做一个练习,让我对基础知识更加熟悉,但这个程序不会像我希望或期望的那样运行。 我已经使用输入等编写了类似的代码,但这是我第一次遇到这种情况,所以我想知道为什么这段代码没有按预期工作

我希望程序会问我
“如果你同意,请键入“1”;如果你拒绝,请键入“0”。
21次,根据键入的
1或0,它会在变量“同意”或“拒绝”中添加一个,当然,在插入输入后会向我显示当前分数,如:

Who wants to play volleyball on sunday from 4pm to 6pm?
If more 12 or more people agree, I will book a field for us.
Type '1' if you agree - Type '0' if you decline.
1
1 YES vs. 0 NO
Type '1' if you agree - Type '0' if you decline.
0
1 YES vs. 1 NO
Type '1' if you agree - Type '0' if you decline.

# and so on...
但它却给了我:

Who wants to play volleyball on sunday from 4pm to 6pm?
If more 12 or more people agree, I will book a field for us.
Type '1' if you agree - Type '0' if you decline.
1
11
11
1 YES vs. 0 NO
Type '1' if you agree - Type '0' if you decline.
0
00
1 YES vs. 1 NO
Type '1' if you agree - Type '0' if you decline.
0
00
1 YES vs. 2 NO
Type '1' if you agree - Type '0' if you decline.
0
00
1 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
11
2 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
11
3 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
11
4 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
11
5 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
11
6 YES vs. 3 NO
Type '1' if you agree - Type '0' if you decline.
1
10
6 YES vs. 4 NO
Type '1' if you agree - Type '0' if you decline.
0
01
7 YES vs. 4 NO
Type '1' if you agree - Type '0' if you decline.
0
00
7 YES vs. 5 NO
Type '1' if you agree - Type '0' if you decline.
1
11
8 YES vs. 5 NO
Type '1' if you agree - Type '0' if you decline.
1
11
9 YES vs. 5 NO
Type '1' if you agree - Type '0' if you decline.
1
11
10 YES vs. 5 NO
Type '1' if you agree - Type '0' if you decline.
1
10
10 YES vs. 6 NO
Type '1' if you agree - Type '0' if you decline.
1
10
10 YES vs. 7 NO
Type '1' if you agree - Type '0' if you decline.
0
00
10 YES vs. 8 NO
Type '1' if you agree - Type '0' if you decline.
0
01
11 YES vs. 8 NO
Type '1' if you agree - Type '0' if you decline.
1
11
12 YES vs. 8 NO
Type '1' if you agree - Type '0' if you decline.
1
11
13 YES vs. 8 NO
Type '1' if you agree - Type '0' if you decline.
1
Cool,I am gonna book a field for us.SEE YOU ON SUNDAY at 4pm.LET'S GOOOOO!!!
Have a nice week until then !
如果您想知道为什么有时会出现
11、00、01或10
: 第二个数字是我必须再次插入作为输入的数字,第一个数字由程序打印出来

如果只有一个数字,
1或0
: 这是我的意见

正如你可能已经注意到的: 我先插入什么或第二个作为输入并不重要。因为某种原因,我不知道只有最后的输入才有意义

(我还没有弄清楚如何可视化(比如:斜体、粗体或颜色)您的代码,这就是为什么我必须在这里稍微解释一下代码的原因…)

我的代码:

print("Who wants to play volleyball on sunday from 4pm to 6pm?")
print("If more 12 or more people agree, I will book a field for us.")
print("Type '1' if you agree - Type '0' if you decline.")

answer = input()
z = int(input(answer))
agreements = 0
declines = 0

for i in range(0, 21):
    try:
        z = int(input(answer))
        if z == 1:
            agreements += 1
            print(agreements, "YES vs.", declines, "NO")
            print("Type '1' if you agree - Type '0' if you decline.")

            answer = input()
        elif z == 0:
            declines += 1
            print(agreements, "YES vs.", declines, "NO")
            print("Type '1' if you agree - Type '0' if you decline.")

            answer = input()
        else:
            raise
    except:
        print("Please type '1' or '0'.Try again.")

if agreements >= 12:
    print("Cool,I am gonna book a field for us."
          "SEE YOU ON SUNDAY at 4pm."
          "LET'S GOOOOO!!!")
else:
    print("Unfortunately we didn't manage to get enough people for booking a field."
          "Maybe next time! :)")

print("Have a nice week until then !")
不管我怎么看,我都找不到问题所在。 这将是一个愚蠢的错误


我提前表示歉意和感谢。

您要求的是更多的
输入()。代码中的每个
input()
都将打印括号中的内容(如果有),并等待用户输入:您

这就是为什么有比你想象的更多的投入——还有很多你认为不应该发生的产出。本质上,您是在提示自己输入一些文本,然后将这些文本用作消息,并打印为代码中下一个输入()的提示

这:

下一个问题:即使给出了无效的输入,for循环也会增加,所以如果输入错误21次,也不会被询问21次“有效”

您可以通过不使用“数字”进行比较和使用函数询问(只要给出无效输入)来简化此操作:

def agreeToPlay():
    """Loop until either 0 or 1 is inputted - remove whitespsaces for convenience.
    Returns True on agreement, else False"""

    # loop with no end
    while True:
        choice = input("Type '1' if you agree - Type '0' if you decline:").strip()

        # only way out of the while is by inputing either 0 or 1:
        if choice in ["1","0"]:
            # only way to break out of the endless loop is valid input
            return choice == "1"  # return True if 1 else False
您可以在主程序中使用此功能:

print("Who wants to play volleyball on sunday from 4pm to 6pm?")
print("If more 12 or more people agree, I will book a field for us.")
print("Type '1' if you agree - Type '0' if you decline.")

agreements = 0
declines = 0
answers = []

# shortcut:
# while agreements + declines < 21 and agreements < 12  # ends as soon as 12 agreed

while agreements + declines < 21: 
    if agreeToPlay(): # ask until valid, then increment the correct one based
                      # on return of True / False
        agreements += 1
    else:
        declines +=1 

    print(agreements, "YES vs.", declines, "NO")

if agreements >= 12:
    print("Cool,I am gonna book a field for us."
          "SEE YOU ON SUNDAY at 4pm."
          "LET'S GOOOOO!!!")
else:
    print("Unfortunately we didn't manage to get enough people for booking a field."
          "Maybe next time! :)") 
print(“谁想在周日下午4点到6点打排球?”)
打印(“如果超过12人或更多人同意,我将为我们预订一个字段。”)
打印(“如果您同意,请键入“1”;如果您拒绝,请键入“0”。)
协议=0
下降=0
答案=[]
#快捷方式:
#协议+下降<21,协议<12#在12达成一致后立即结束
协议+下降<21:
如果agreeToPlay():#询问直到有效,然后基于
#返回真/假时
协议+=1
其他:
下降+=1
打印(协议“是”与“否”,拒绝“否”)
如果协议>=12:
打印(“酷,我要为我们预订一块地。”
“星期天下午4点见。”
“让我们加油!!!”)
其他:
print(“不幸的是,我们没有找到足够的人来预订场地。”
“也许下次吧!:)”)

你不应该做
z=int(输入())
z=int(答案)
而不是
z=int(输入(答案))
?哦,是的,实际上使用
z=int(答案)
解决了我的问题。这是我的错,我有点加倍,使用输入和回答。非常感谢。但现在又出现了另一个问题。我的try and except现在似乎不起作用,因为每当我编写与0或1不同的内容时,程序都以
结束,请键入“1”或“0”。再试一次。
(循环范围的倍)
不幸的是,我们没有管理足够的人来预订字段。也许下次吧!:)在那之前祝你一周愉快不要求我再次插入通常应该插入的输入,对吗?哦,这很有意义。当我写这部分代码的时候我在想什么…嗯。哦,哇,这很有趣。它与我的atempt完全不同。谢谢你的见解和你的时间帮助我。我很感激。谢谢你,阿特纳先生!
print("Who wants to play volleyball on sunday from 4pm to 6pm?")
print("If more 12 or more people agree, I will book a field for us.")
print("Type '1' if you agree - Type '0' if you decline.")

agreements = 0
declines = 0
answers = []

# shortcut:
# while agreements + declines < 21 and agreements < 12  # ends as soon as 12 agreed

while agreements + declines < 21: 
    if agreeToPlay(): # ask until valid, then increment the correct one based
                      # on return of True / False
        agreements += 1
    else:
        declines +=1 

    print(agreements, "YES vs.", declines, "NO")

if agreements >= 12:
    print("Cool,I am gonna book a field for us."
          "SEE YOU ON SUNDAY at 4pm."
          "LET'S GOOOOO!!!")
else:
    print("Unfortunately we didn't manage to get enough people for booking a field."
          "Maybe next time! :)")