Проблема с бесконечным циклом while в Python 3.1 - PullRequest
0 голосов
/ 01 июня 2018

Я создаю игру турагента на Python 3.1.Я обнаружил ошибку в цикле while.Он будет постоянно повторять ответ print ().Я знаю, что это потому, что это правда, пока здесь есть ответ для людей, но я понятия не имею, как это исправить.

people = int(input("Will you be travelling by yourself (1), or as a group of 
two (2)?: "))
while people: 
    if people == 1:
        print("\nAh, a holiday for one! How adventurous.")
    elif people == 2:
        print("\nOoh, bringing a friend! Sounds like fun!")
    else:
        print("\nPlease enter either 1 or 2 to determine the number of 
        travellers.")
        people = int(input("Will you be travelling by yourself (1), or as a 
        group of two (2)?: "))

1 Ответ

0 голосов
/ 01 июня 2018

Нет никаких проблем с версией Python относительно вашего вопроса. Проблема в том, что цикл будет выполняться бесконечно, потому что нет условия для выхода из цикла. Так что выйти из цикла после оператора print вставьте breakключевое слово следующим образом.

people = int(input("Will you be travelling by yourself (1), or as a group of two (2)?: "))
while people: 
    if people == 1:
        print("\nAh, a holiday for one! How adventurous.")
        break
    elif people == 2:
        print("\nOoh, bringing a friend! Sounds like fun!")
        break
    else:
        print("\nPlease enter either 1 or 2 to determine the number of travellers.")
        people = int(input("Will you be travelling by yourself (1), or as a group of two (2)?: "))
...