【发布时间】:2018-05-08 09:54:09
【问题描述】:
我刚开始学习编码。
我正在尝试编写这个简单的计数器。它在第一次运行时起作用,但是当循环调用“while()”时,它会重置“r”和列表“we_list”“you_list”。即使在循环之后,我也不知道如何存储它们的值。
def begin():
r = 1
print("This is a counter for the game Belote")
print("Round " + str(r))
we_list = []
you_list = []
we = int(input("Enter score for 'We' "))
we_list.append(we)
we_sum = sum(we_list)
you = int(input("Enter score for 'you' "))
you_list.append(you)
you_sum = sum(you_list)
print("WE " + str(we_sum))
print("YOU " + str(you_sum))
r += 1
while we_sum or you_sum < 151:
begin()
else:
print("End of game ")
exit()
begin()
编辑:
我根据建议编辑了代码,并设法修复了 r 和列表,但是现在我遇到的问题是它在 151 之后没有跳出循环。
we_list = []
you_list = []
def begin(r):
print("This is a counter for the game Belote")
print("Round " + str(r))
we = int(input("Enter score for 'We' "))
we_list.append(we)
we_sum = sum(we_list)
you = int(input("Enter score for 'you' "))
you_list.append(you)
you_sum = sum(you_list)
print("WE " + str(we_sum))
print("YOU " + str(you_sum))
r += 1
while we_sum or you_sum < 151:
begin(r)
else:
print("End of game ")
exit()
r=1
begin(r)
【问题讨论】:
-
递归根本不适合在这里使用。
-
另外,
we_sum or you_sum < 151不会按照你的想法做,请参阅 stackoverflow.com/questions/15112125/…
标签: python loops while-loop reset