【问题标题】:Python Doesn't return to while loop [closed]Python不返回while循环[关闭]
【发布时间】:2014-08-11 21:28:49
【问题描述】:

我是 Python 新手,我的程序有点问题。每当我输入大于第一个数字的第二个数字时,“mCounter”应该设置为false,并且由于有一个while循环,它应该要求我再次输入位数。由于某种原因,这不会发生。每当我输入第二个大于第一个的数字时,程序就会停止。 任何帮助将不胜感激。 谢谢!

import random 
#Introduction
print('Choose the mode that you would like to enter by typing the letters in the brackets')
problem = input('Multiplication(M) Addition (A) Subtraction (S) Division (D): ')
#Multiplication
if problem == 'M' or problem == 'm':
    mCounter = False
    while mCounter == False:
        mInput1 = int(input('Enter the amount of digits you would like in the first number you are multiplying.\nThe first number should be greater or equal to the second number: '))
        mInput2 = int(input('Enter the amount of digits you would like in the second factor: '))
        mCounter = True
        if mInput2 > mInput1:
            print('The first number MUST be greater or equal to the second number. Please try again!')
            mCounter == False
        else:
            print('nothing')

【问题讨论】:

  • 删除mCounter == False中的双等号

标签: python loops while-loop skip


【解决方案1】:

要设置mCounter 的值,请执行以下操作:

mCounter = False

而不是这样:

mCounter == False

您的代码只是比较mCounterFalse 的值,然后忽略比较的结果。

【讨论】:

    【解决方案2】:

    代替:

    mCounter == False
    

    你想要的:

    mCounter = False
    

    您需要分配,而不是条件检查。

    【讨论】:

      【解决方案3】:

      声明mCounter == False 不会改变mCounter。您必须使用= 进行分配。

      if mInput2 > mInput1:
          print('The first number MUST be greater or equal to the second number. Please try again!')
          mCounter = False #<-- use = instead of ==
      

      【讨论】:

        猜你喜欢
        • 2013-06-07
        • 2019-06-07
        • 1970-01-01
        • 2018-08-15
        • 2020-05-27
        • 1970-01-01
        • 2016-07-12
        • 2021-02-01
        • 2015-02-24
        相关资源
        最近更新 更多