Python 重置变量 - While 循环
Python resets variables - While loop
我刚开始学习编码。
我正在尝试编写这个简单的计数器。它适用于第一个 运行,但是当循环调用 "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)
您正在 begin 函数中初始化 r,we_list and you_list
,因此每次调用 begin 时它们都被初始化为 r=1, you_list=[] and we_list = []
。在 begin 函数之外初始化它们。
你的设计有点乱,你应该把"round"逻辑隔离成一个专用的函数,return这些值。
另外如果你不需要跟踪每个增加的值,你不需要保留列表,你可以简单地直接加起来。
def round(we, you):
we_in = int(input("Enter score for 'We' "))
we = we + we_in
you_in = int(input("Enter score for 'you' "))
you = you + you_in
print("WE " + str(we))
print("YOU " + str(you))
return [we, you]
def begin():
r = 1
print("This is a counter for the game Belote")
we_sum = 0
you_sum = 0
while we_sum or you_sum < 151:
print("Round " + str(r))
r += 1
[we_sum, you_sum] = round(we_sum, you_sum)
else:
print("End of game ")
exit
r
是一个局部变量,因此每次 begin()
调用自身时,新的 begin()
都会得到一个新的 r
。
您可以使 r
、we_list
和 you_list
成为全局变量(在 begin()
之外或使用 global
关键字声明它们),它将保存价值。
修复您的代码发送 r 作为参数
def begin(r):
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(r)
else:
print("End of game ")
exit()
r=1
begin(r)
我刚开始学习编码。
我正在尝试编写这个简单的计数器。它适用于第一个 运行,但是当循环调用 "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)
您正在 begin 函数中初始化 r,we_list and you_list
,因此每次调用 begin 时它们都被初始化为 r=1, you_list=[] and we_list = []
。在 begin 函数之外初始化它们。
你的设计有点乱,你应该把"round"逻辑隔离成一个专用的函数,return这些值。
另外如果你不需要跟踪每个增加的值,你不需要保留列表,你可以简单地直接加起来。
def round(we, you):
we_in = int(input("Enter score for 'We' "))
we = we + we_in
you_in = int(input("Enter score for 'you' "))
you = you + you_in
print("WE " + str(we))
print("YOU " + str(you))
return [we, you]
def begin():
r = 1
print("This is a counter for the game Belote")
we_sum = 0
you_sum = 0
while we_sum or you_sum < 151:
print("Round " + str(r))
r += 1
[we_sum, you_sum] = round(we_sum, you_sum)
else:
print("End of game ")
exit
r
是一个局部变量,因此每次 begin()
调用自身时,新的 begin()
都会得到一个新的 r
。
您可以使 r
、we_list
和 you_list
成为全局变量(在 begin()
之外或使用 global
关键字声明它们),它将保存价值。
修复您的代码发送 r 作为参数
def begin(r):
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(r)
else:
print("End of game ")
exit()
r=1
begin(r)