【问题标题】:While loop won't stop looping虽然循环不会停止循环
【发布时间】:2020-01-15 03:58:45
【问题描述】:

我对 python 或一般编码有点陌生,我在我的 while 循环中遇到了问题。我想指出,while 循环仅在变量“loop”= True 时才有效

loop = True #Makes the asking sequence work


def loop_again(): #Makes the program ask if you want to continue
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again

当我在程序询问我是否要再次执行此操作时写“否”时,它仍然循环序列,即使当我键入“否”时变量“循环”应该是假的,这应该是停止循环。

完整代码(代码底部的while循环):

#Solving for the area in a shape automatically

import math

loop = True #Makes the asking sequence work


def loop_again(): #Makes the program ask if you want to continue
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again



def sqr_area(): #Asking sequence for the area of the square
    if choose == "Square":
        a = float(input("Input the length of the side here: "))
        print(a ** 2)
        loop_again()

def rec_area(): #Asking sequence for the area of the rectangle
    if choose == "Rectangle":
        a = float(input("Input the length of the long sides here: "))
        b = float(input("Input the length of the short sides here: "))
        print(a * b)
        loop_again()

def tri_area(): #Asking sequence for the area of the triangle
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)
    loop_again()

def cir_area(): #Asking sequence for the area of the circle
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)
    loop_again()

while loop == True: #While loop, asking sequence
    choose = str(input("Input what shape that you want to figure out the area of here: "))
    if choose == "Square":
        sqr_area()
    elif choose == "Rectangle":
        rec_area()
    elif choose == "Triangle":
        tri_area()
    elif choose == "Circle":
        cir_area()
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        choose

提前致谢!

【问题讨论】:

标签: python loops


【解决方案1】:

当您应该使用循环时不要使用递归,loop_again 应该返回一个值,而不是全局设置 loop

import math


# Returns true once the input is Yes or false once the input is No
def loop_again():
    while True:
        response = str(input("Do you want to do this again? Yes or No: "))
        if response == "Yes":
            return True
        elif response == "No":
            return False
        else:
            print("Please answer yes or no: ")

loop_again 应该在相关的*_area 函数返回之后被调用,而不是在每个函数内部。这些功能不需要知道或关心 关于choose的值;仅在打算调用它们时才调用它们。

# Print the area of a square
def sqr_area():
    a = float(input("Input the length of the side here: "))
    print(a ** 2)


# Print the area of a rectangle
def rec_area():
    a = float(input("Input the length of the long sides here: "))
    b = float(input("Input the length of the short sides here: "))
    print(a * b)


# Print the area of a triangle
def tri_area():
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)


# Print the area of a circle
def cir_area():
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)

最后的循环可以无限期地运行,直到loop_again 返回True

# Loop until the user chooses to not run again
while True:
    choose = input("Input what shape that you want to figure out the area of here: ")
    if choose == "Square":
        sqr_area()
    elif choose == "Rectangle":
        rec_area()
    elif choose == "Triangle":
        tri_area()
    elif choose == "Circle":
        cir_area()
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        continue

    if loop_again():
        break

【讨论】:

    【解决方案2】:

    loop_again() 函数中你必须在开头添加这个:

    global loop
    

    否则,该变量被视为本地,不会对外部范围内的其他 loop 变量产生任何影响。

    【讨论】:

      【解决方案3】:

      这是一个上下文变量问题。您在loop_again() 中拥有的loop 变量和while 循环中的变量是不同的。 在 python 中,函数内部的每个变量都是局部变量,除非它是一个参数,或者如果您在 main 和函数内部使用 global variable

      所以让它成为全局的或者在函数内部传递和返回

      #Solving for the area in a shape automatically
      
      import math
      global loop 
      loop = True #Makes the asking sequence work
      
      
      def loop_again(): #Makes the program ask if you want to continue
          global loop 
          loop_again = str(input("Do you want to do this again? Yes or No: "))
          if loop_again == "Yes":
              loop = True
          elif loop_again == "No":
              loop = False
          else:
              print("Please answer yes or no: ")
              loop_again
      
      
      
      def sqr_area(): #Asking sequence for the area of the square
          if choose == "Square":
              a = float(input("Input the length of the side here: "))
              print(a ** 2)
              loop_again()
      
      def rec_area(): #Asking sequence for the area of the rectangle
          if choose == "Rectangle":
              a = float(input("Input the length of the long sides here: "))
              b = float(input("Input the length of the short sides here: "))
              print(a * b)
              loop_again()
      
      def tri_area(): #Asking sequence for the area of the triangle
          a = float(input("Input the length of the side: "))
          b = float(input("Input the length of the height: "))
          print((a * b) / 2)
          loop_again()
      
      def cir_area(): #Asking sequence for the area of the circle
          r = float(input("Length of the radius: "))
          print((r ** 2) * math.pi)
          loop_again()
      
      while loop == True: #While loop, asking sequence
          global loop 
          choose = str(input("Input what shape that you want to figure out the area of here: "))
          if choose == "Square":
              sqr_area()
          elif choose == "Rectangle":
              rec_area()
          elif choose == "Triangle":
              tri_area()
          elif choose == "Circle":
              cir_area()
          else:
              print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
              choose
      

      你需要在每个函数中说明循环是全局的,否则python你将它解释为一个局部变量。

      另一种方法是:

      import math
      loop = True #Makes the asking sequence work
      
      
      def loop_again(loop ): #Makes the program ask if you want to continue
          loop_again = str(input("Do you want to do this again? Yes or No: "))
          if loop_again == "Yes":
              loop = True
          elif loop_again == "No":
              loop = False
          else:
              print("Please answer yes or no: ")
              loop_again
          return loop
      
      
      
      def sqr_area(loop): #Asking sequence for the area of the square
          if choose == "Square":
              a = float(input("Input the length of the side here: "))
              print(a ** 2)
              loop = loop_again()
          return loop
      
      def rec_area(loop): #Asking sequence for the area of the rectangle
          if choose == "Rectangle":
              a = float(input("Input the length of the long sides here: "))
              b = float(input("Input the length of the short sides here: "))
              print(a * b)
              loop = loop_again(loop)
          return loop
      
      
      def tri_area(loop): #Asking sequence for the area of the triangle
          a = float(input("Input the length of the side: "))
          b = float(input("Input the length of the height: "))
          print((a * b) / 2)
          loop = loop_again()
      
      def cir_area(loop): #Asking sequence for the area of the circle
          r = float(input("Length of the radius: "))
          print((r ** 2) * math.pi)
          loop = loop_again()
      
      while loop == True: #While loop, asking sequence
          choose = str(input("Input what shape that you want to figure out the area of here: "))
          if choose == "Square":
              loop = sqr_area(loop )
          elif choose == "Rectangle":
              loop = rec_area(loop )
          elif choose == "Triangle":
              loop = tri_area(loop )
          elif choose == "Circle":
              loop = cir_area(loop )
          else:
              print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
              choose```
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-16
        • 2014-06-06
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 2015-09-14
        • 2011-12-03
        相关资源
        最近更新 更多