【问题标题】:How to fix Python restarting whenever I start program on IDLE environment?每当我在 IDLE 环境中启动程序时,如何修复 Python 重启?
【发布时间】:2015-10-04 20:08:26
【问题描述】:
import random

winning_conditon = 0
no_of_guesses = 0
comp_guess = random.randint(1,100)

while (no_of_guesses == 11) or (winning_conditon == 1):
    user_guess = int(input("What is your guess? "))
    if user_guess < 1 or user_guess > 100:
        print("Your number is invalid!")

    elif comp_guess == user_guess:
        print("Well Done!")
        winning_condition = 1
    elif comp_guess < user_guess:
        print("Your number is too high!")
    elif comp_guess > user_guess:
        print("Your number is too low!")

    no_of_guesses = no_of_guesses + 1
    print(no_of_guesses)
print("You haven't guessed the right amount of times or u won.")

每当我启动 python IDLE(我使用的是 Portable Python 3.2.5.1 (http://portablepython.com/wiki/PortablePython3.2.5.1/))时,它都会出现一个重新启动消息,然后它会显示一个“=”符号并且不会继续该程序。你知道解决办法吗?

【问题讨论】:

  • 这和你发布的代码有关系吗?
  • 删除初始缩进后,程序运行。从 IDLE 编辑器运行时,RESTART 行是正常的。这意味着程序正在一个新的命名空间中运行,就像您从命令行运行一样。由于 while 条件为假,循环永远不会运行。您需要 not 用户的回答。最后一行被打印出来,程序退出。我在打印行之后看到的是&gt;&gt;&gt; ,也和用户的回答一样。如果您在控制台中运行 python -i &lt;filename&gt;,您将看到相同的输出。

标签: python shell python-idle


【解决方案1】:

当我运行你的程序时,我得到这个输出:

Python 3.2.5 (default, May 15 2013, 23:06:03) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
You haven't guessed the right amount of times or u won.
>>> 

这非常好,符合预期。

我会这样修改你的程序:

while not (no_of_guesses == 11 or winning_conditon == 1)

while not 将等同于 until

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2019-09-10
    • 2012-02-06
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    相关资源
    最近更新 更多