简体   繁体   中英

Issue With Infinite While Loop in Python 3.1

I am creating a travel agent game in Python 3.1. I have reached an error with my while loop. It will constantly repeat the print() response. I know this is because it is true as long as here is a response for people, but I have no clue how to fix it.

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)?: "))

There is no any issue with the python version regard to you question.The problem is that, loop will run infinitely because there is no condition to exit from the loop.So to exit from the loop after print statement insert break keyword as follows.

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)?: "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM