Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm trying to let program run until user won't type "No". Here's code I've already done:

print("What language would you prefer?\n e for English, s for spanish, f for french")

choice= input("Your choice\n")

english= "Hello iso-3166-2: en-us"

french= "Salut! iso-3166-2:fr"

spanish= "Ola iso-3166-2:es"

if choice == "e":

    print (english)

elif choice == "f":

    print (french)

elif choice == "s":

    print (spanish) 

while True:

    res= input("Do you want to choose another language? Yes/No:")

    if res == "No":

        break

1 Answer

0 votes
by (25.1k points)

Use an infinite loop with one of the conditional statements resulting in a break statement. To quit the loop and the program.

You can do it like this:

 english= "Hello iso-3166-2: en-us"

french= "Salut! iso-3166-2:fr"

spanish= "Ola iso-3166-2:es"

 

print("What language would you prefer?\n e for English, s for spanish, f for french")

 

choice = input("Your choice\n")

 

while True:

    if choice == "e":

    print(english)

    elif choice == "f":

       print(french)

    elif choice == "s":

    print(spanish)

res = input("Do you want to choose another language? Yes/No:")

    if res == 'Yes':

    print("What language would you prefer?\n e for English, s for spanish, for french")

     choice = input("Your choice\n")

    elif res == 'No':

    print ("Goodbye")

    break 

Output:

image

Related questions

0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)
0 votes
0 answers
asked Jul 17, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)

Browse Categories

...