简体   繁体   中英

How to repeat a command in python

I am just starting in python i want to make if the user wrote a number in the name he reapeats the question until he writes a string value and if he writes a the string value he asks him the number

while True :
    
      name = input("What is your name: ").capitalize()
      if any(char.isdigit() for char in name):
      print("Please do not include digits in your name.")     

 try:
   number = int(input("What is your number: "))
   break
 except ValueError:
    print(" you have to write a number")

Do something like this

while True :
    name = input("What is your name: ").capitalize()  # get input
    if any(char.isdigit() for char in name):  # check input
        print("Please do not include digits in your name.")
        continue # start over (skip iteration and start new iteration of loop)
    while True:  # loop for second input.
        number = input("What is your number: ")  # get input. 
        try:
            number = int(number)
            break  # stop the loop if input is a number.
        except ValueError:
            print(" you have to write a number")
            # if input is not a number. loop will run again (run next iteration)
    break  # when we got both inputs right. stop the main loop.

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