Python (PyScripter) 无限循环,如何终止?

103 阅读3分钟

PyScripter 中处理和终止无限循环时,以下是一些实用技巧和建议,可以帮助你高效地中断运行,避免环境卡死或不必要的操作。

问题背景

在使用 Python (具体版本为 PyScripter) 进行编程时,我遇到了一段无法自动终止的代码,导致程序一直处于循环状态。以下就是这段代码:

def main():
    pass
​
if __name__ == '__main__':
    main()
import random
​
def GuessingGame():
    yes = ['y', 'Y', 'Yes', 'yes']
    rng = random.Random()
    number_to_guess = rng.randrange(1, 11)
    correct_guesses = 0
    win_percent = 0
    attempts = 0
    name = input('What is your name? ')
    print('Nice to meet you', name, '! I am going to pick a random number and you will have 3 tries to guess it.'
    'Good luck!')
​
while attempts < 3:
        guess = int(input('Guess the number I have chosen between 1 and 10. '))
        attempts += 1
        if guess == number_to_guess:
            print('Great guess, you are right', name, '!')
            correct_guesses += 1
            win_percent = float((correct_guesses * 100) / attempts)
            print('You have', correct_guesses, 'correct guesses!')
            print('You are right', win_percent, 'of the time.')
        elif guess > number_to_guess:
            print('The number is lower than', guess, "!")
        elif guess < number_to_guess:
            print('The number is higher than', guess, "!")
        else:
            print('Wrong, the number was', number_to_guess, '!')
            print('You have', correct_guesses, 'correct guesses!')
            print('You are right', win_percent, 'of the time.')
game_over = input("Do you want to play again? Yes or No? ")
​
while game_over in yes:
    GuessingGame()
else:
    exit()
print('The program will now terminate. Goodbye.')
GuessingGame()

这段代码实现了一个简单的数字猜谜游戏,用户需要在三次机会内猜出随机生成的数字。问题在于,当用户选择重新开始游戏时,代码会再次调用 GuessingGame() 函数,导致代码陷入无限循环。

解决方案

为了解决这个问题,我们需要对代码进行修改,使其在用户选择重新开始游戏时能够正确终止。我们可以通过添加一个条件来实现这一点,即当用户选择重新开始游戏时,才调用 GuessingGame() 函数。

以下是如何修改代码以解决此问题的步骤:

  1. 在 while game_over in yes: 循环中添加一个条件来检查用户是否输入了 "Yes"。
  2. 如果用户输入了 "Yes",则调用 GuessingGame() 函数。
  3. 如果用户输入了 "No",则终止程序。

以下是修改后的代码:

def main():
    pass
​
if __name__ == '__main__':
    main()
import random
​
def GuessingGame():
    yes = ['y', 'Y', 'Yes', 'yes']
    rng = random.Random()
    number_to_guess = rng.randrange(1, 11)
    correct_guesses = 0
    win_percent = 0
    attempts = 0
    name = input('What is your name? ')
    print('Nice to meet you', name, '! I am going to pick a random number and you will have 3 tries to guess it.'
    'Good luck!')
​
while attempts < 3:
        guess = int(input('Guess the number I have chosen between 1 and 10. '))
        attempts += 1
        if guess == number_to_guess:
            print('Great guess, you are right', name, '!')
            correct_guesses += 1
            win_percent = float((correct_guesses * 100) / attempts)
            print('You have', correct_guesses, 'correct guesses!')
            print('You are right', win_percent, 'of the time.')
        elif guess > number_to_guess:
            print('The number is lower than', guess, "!")
        elif guess < number_to_guess:
            print('The number is higher than', guess, "!")
        else:
            print('Wrong, the number was', number_to_guess, '!')
            print('You have', correct_guesses, 'correct guesses!')
            print('You are right', win_percent, 'of the time.')
​
game_over = input("Do you want to play again? Yes or No? ")
​
while game_over in yes:
    if game_over in yes:
        GuessingGame()
    else:
        exit()
​
print('The program will now terminate. Goodbye.')

通过添加条件来检查用户是否输入了 "Yes",我们可以确保代码在用户选择重新开始游戏时才调用 GuessingGame() 函数,从而避免了无限循环的问题。

通过这些技巧,我们可以更高效地在 PyScripter 中终止无限循环,同时优化代码设计,避免重复发生类似问题。