【问题标题】:how to stop this loop from repeating [closed]如何阻止此循环重复[关闭]
【发布时间】:2021-03-20 04:50:30
【问题描述】:

这是个错误:

while True:
    if french_grade == 1 and PE_grade == 1 and Math_grade == 1 and english_grade ==1:
        music grade == 0

我所要做的就是在音乐分级后添加一个break 行,这样它就可以阻止循环重复。

应该是这样的:

while True:
    if french_grade == 1 and PE_grade == 1 and Math_grade == 1 and english_grade ==1:
        music grade == 0
        break

【问题讨论】:

  • 请提供预期的MRE - Minimal, Reproducible Example。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。这也让我们可以在您的上下文中测试任何建议。您发布了超过 200 行代码;这对你提出的问题来说太过分了。
  • 您发布的程序挂起等待输入。不要指望我们提供测试数据:只需将您的 input 替换为引发问题的测试用例即可。
  • 我们还希望您在错误点之前跟踪可疑值,并尽最大努力将它们追溯到它们的源头。您对它们如何获得这些值感到困惑?

标签: python loops


【解决方案1】:
while True:
    if english_grade == 1 and math_grade == 1 and SocialStudies_grade == 1 and art_grade == 1 and science_grade == 1:
        french_Grade = 0

这将永远运行 ..setting french=0

你也需要break循环和其他这种循环

【讨论】:

    【解决方案2】:

    来自代码

    if english_grade == 1 and math_grade == 1 and SocialStudies_grade == 1 and art_grade == 1 and science_grade == 1:
            french_Grade = 0
    

    编码

    elif english_grade == 1 and math_grade == 1 and SocialStudies_grade == 1 and art_grade == 1 and science_grade == 0 and PE_grade == 1:
            french_Grade = 0
    

    来自代码

    if english_grade == 1 and math_grade == 1 and SocialStudies_grade == 1 and art_grade == 1 and science_grade == 1:
                music_Grade = 0
    

    编码

    art_grade == 1 and science_grade == 1 and french_grade == 0 and PE_grade == 0:
                    music_Grade = 0
    

    您需要将代码移到 while 循环之外。
    而且你没有让用户输入PE Grade。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2016-07-01
      • 2020-04-09
      • 1970-01-01
      • 2017-10-05
      • 2012-09-21
      • 1970-01-01
      相关资源
      最近更新 更多