【问题标题】:Python resets variables - While loopPython 重置变量 - While 循环
【发布时间】: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)

【问题讨论】:

标签: python loops while-loop reset


【解决方案1】:

你的设计有点乱,你应该把“round”逻辑隔离到一个专用函数中,并返回这些值。

此外,如果您不需要跟踪每个添加的值,则不需要保留列表,您可以简单地直接求和。

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

【讨论】:

  • 我试过你的代码,但是返回以下错误:while we_sum or you_sum
  • 我刚刚修正了一个错字
【解决方案2】:

您在 begin 函数中初始化 r,we_list and you_list,因此当每次初始化为 r=1, you_list=[] and we_list = [] 时,从调用 begin 时。在 begin 函数之外初始化它们。

【讨论】:

    【解决方案3】:

    r 是一个局部变量,所以每次begin() 调用自己时,新的begin() 都会得到一个新的r

    您可以将rwe_listyou_list 设为全局变量(在begin() 之外声明它们或使用global 关键字),它将保存这些值。

    【讨论】:

    • 当我在函数外部声明它们时,我得到的是:UnboundLocalError: local variable 'r' referenced before assignment
    • 尝试在 begin() 的第一行添加global r,这样它就知道应该使用外部定义的r
    【解决方案4】:

    修复您的代码发送 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 2020-05-18
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多