If Yes Is Entered Run the Program Again Python3

Use while true with if statement and suspension argument to create While loop yes or no in Python. Simple if while status equal to "N" then wait for the user input of Y before exiting.

Example While loop yes or no in Python

Elementary example code using 2 while loops. If the user inputs the value "no" and so pause the loops.

          while Truthful:     # your code     cont = input("Another one? yes/no > ")      while cont.lower() not in ("yeah", "no"):         cont = input("Another one? yes/no > ")      if cont == "no":         impress("Break")         interruption                  

Output:

While loop yes or no Python

More examples

While loop in python for do yous want to continue.

          while True:     # some lawmaking here     if input('Practise You Want To Continue? ') != 'y':         break        

Output:

Do Y'all Desire To Continue? y
Do Yous Want To Go on? n

OR

          while input("Do You Want To Go on? [y/northward]: ") == "y":     # do something     impress("doing something")        

Output: Do You Want To Continue? [y/n]: northward

Long lawmaking with role

Best to go along office definition split from the loop for clarity. Also, otherwise, information technology will be read in every loop wasting resources.

          def yes_or_no(question):     reply = str(input(question + ' (y/n): ')).lower().strip()     if reply[0] == 'y':         render one     elif reply[0] == 'due north':         return 0     else:         render yes_or_no("Please Enter (y/due north) ")   impress("started") while Truthful:     # DRAW PLOT HERE;     print("See plot....")     if yes_or_no('Practice yous like the plot'):         intermission print("done")                  

Output:

started
See plot….
Do you like the plot (y/north): y
done

Loop the question to allow for repeated incorrect input

                      answer = None      while answer not in ("yes", "no"):          answer = input("Enter yes or no: ")          if answer == "yes":               # Do this.          elif answer == "no":               # Do that.          else:          	impress("Please enter yeah or no.")                  

Practice comment if yous accept any doubts or suggestions on this Python while loop code.

Note: IDE: PyCharm 2021.three (Customs Edition)

Windows x

Python 3.10.1

All Python Examples are in Python 3, so Perhaps its different from python 2 or upgraded versions.

pryornordiris.blogspot.com

Source: https://tutorial.eyehunts.com/python/while-loop-yes-or-no-python-example-code/

Belum ada Komentar untuk "If Yes Is Entered Run the Program Again Python3"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel