【问题标题】:Not understanding for loops不理解 for 循环
【发布时间】:2020-09-26 17:26:10
【问题描述】:

我需要使用 for 循环找到完美立方体的立方根,但我不知道为什么我的代码不起作用:

n = int(input('n = '))
for guess in range(0, n+1):
    if guess**3 != n:
        guess = guess + 1
    if guess**3 == n:
        print(guess, 'is the cube root of', n)
    if guess**3 != n:
        print("not a perfect cube")

如果我输入数字 8(例如),它会打印出来:

n = 8
not a perfect cube
2 is the cube root of 8
2 is the cube root of 8
not a perfect cube
not a perfect cube
not a perfect cube
not a perfect cube
not a perfect cube
not a perfect cube

我想知道是否有人可以帮助我意识到我做错了什么。

【问题讨论】:

  • 你不需要在for循环中手动改变猜测,循环会自动完成
  • 正如@ChristianSloper 提到的,您正在修改guess 两次。此外,您可能希望将其重写为 if / else 块而不是多个 if 语句。最后,现在,每次循环执行时都会调用最后一个if guess**3 != n:。在评估了所有可能的猜测之后,您似乎希望它只发生一次。
  • 怎么不行?你期望什么输出?

标签: python python-3.x loops cube


【解决方案1】:

n**(1/3) 也会给出 n 的立方根。

您的代码可以通过以下方式改进:

    n = int(input('n = '))
    for guess in range(0, n+1):
        if guess**3 == n:
            print(guess, 'is the cube root of', n)
            break
    else:
        print("not a perfect cube")

你不需要guess=guess+1

【讨论】:

    【解决方案2】:

    for 循环中使用range 将增加guess。你可以拿出这个:

        if guess**3 != n:
            guess = guess + 1
    

    您不想检查它是否不是每个guess 的完美立方体。一旦你经历了所有的guesses,你就会知道它不是一个完美的立方体。因此这个逻辑需要在for 循环之后发生。

        if guess**3 != n:
            print("not a perfect cube")
    

    我将root 存储在一个变量中并检查它。

    n = int(input('n = '))
    root = -1;
    for guess in range(0, n):
        if guess**3 == n:
            root = guess
    
    if root > -1:
        print(root, 'is the cube root of', n)
    else:
        print("not a perfect cube")
    

    【讨论】:

      【解决方案3】:

      您可以改用此代码:

      n = int(input("Enter an integer: "))
      
      for ans in range(0, abs(n) + 1):
          if ans ** 3 == abs(n):
              break
      if ans ** 3 != abs(n):
          print (n, 'is not a perfect cube!')
      else:
          if n < 0:
              ans = -ans
          print('Cube root of ' + str(n) + ' is ' + str(ans))
      

      【讨论】:

        【解决方案4】:

        你想要做的是:

        found = false
        for guess in range(0,n+1):
            if guess**3 == n:
                print("The cube root of ", n, " is ", guess)
                found = true
                break
        if not found:
            print("The number ", n, " is not a perfect cube.")
        
                
              
        

        【讨论】:

          【解决方案5】:

          for 循环会增加值,在这种情况下是猜测。猜测 = 猜测 + 1 会搞砸的。

          代码:

          n = int(input('n = '))
          for guess in range(0, n+1):
              if guess**3 == n:
                  print(guess, 'is the cube root of', n)
              if guess**3 != n:
                  print("not a perfect cube")
          

          您也可以将第二个 if 条件更改为 elif,这样它只会在第一个条件不满足时运行。

          n = int(input('n = '))
          for guess in range(0, n+1):
              if guess**3 == n:
                  print(guess, 'is the cube root of', n)
              elif guess**3 != n:
                  print("not a perfect cube")
          

          最后,您可以将它存储在一个列表中,[number_found, isFound],如下所示。

          代码:

          n = int(input('n = '))
          found = [None, False]
          for guess in range(0, n+1):
              if guess**3 == n:
                  found = [guess, True]
          
          if found[1]:
              print(found[0], 'is the cube root of', n)
          else:
              print("not a perfect cube")
          

          【讨论】:

            【解决方案6】:

            你的意思是写这个吗:

            n = int(input('n = '))
            for guess in range(1, n + 1):
                if guess ** 3 == n:
                    print(guess, 'is the cube root of', n)
                    break
            else:
                print("not a perfect cube")
            

            正如已经指出的那样,拥有for guess in range(...): 的原因是for loop 会为您增加guess

            我已将失败案例移至for 循环的else: 子句。

            【讨论】:

              【解决方案7】:

              因为你的第一个和第三个 if 语句是一样的 试试这个

              n = int(input('n = '))
              j = 0
              for guess in range(0, n + 1):
              if guess**3 == n:
                  j = guess
              else:
                  guess += 1
              
              if j == 0:
                  print("not a perfect cube")
              else:
                  print(j, 'is the cube root of', n)
              

              【讨论】:

                【解决方案8】:

                你不应该把guess = guess+1放在for循环的第一个if中。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-10-03
                  • 1970-01-01
                  • 2017-09-11
                  • 2013-10-15
                  • 2023-03-11
                  • 2016-01-05
                  • 2019-05-01
                  相关资源
                  最近更新 更多