我一直在练习让我对基础知识更加熟悉,但是这个程序不会按照我想要或期望它运行。 我已经使用输入等编写了类似的代码但是第一次发生在我身上,所以我想知道为什么这段代码没有按预期工作。
我希望程序问我"Type '1' if you agree - Type '0' if you decline."
21次,并根据输入1 or 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 or 10
:
第二个数字是我必须再次作为输入插入的,第一个数字由程序打印。
如果只有一个号码1 or 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 !")
无论我如何看待它,我都无法在这里找到问题。 这将是一个如此愚蠢的错误...
我事先道歉并表示感谢。
答案 0 :(得分:0)
你要求更多input()
,那么你应该。代码中的每个input()
都会打印括号中的内容(如果有的话)并等待用户输入:你。
这就是为什么你认为有更多的输入 - 你认为不应该发生很多输出。基本上你是在提示自己输入一些文本,然后将其用作信息并打印为代码中下一个输入()的提示......
此:
answer = input() # input-prompt. no output. will store inputted thing in answer
z = int(input(answer)) # input-prompt. Will print content of 'answer' as message
# and wait for another input of you
agreements = 0
declines = 0
# etc..... same for your inner loops.
下一个问题:即使输入的输入无效,你的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! :)")