0
nameQuestion = "Whats ur name?"
print(nameQuestion)
name = input()
ageAuestion = "How old r u?"
print(ageAuestion)
age = input()
print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
answer = input()
if answer == "Yes":
    print("Name: " + name + ", " + "Age: " + age)
else:
    print("Error")

I need to restart code if answer == "no". How i can do it

Fer Fooy
  • 91
  • 5

5 Answers5

2

This will do:

while True:
    nameQuestion = "Whats ur name?"
    print(nameQuestion)
    name = input()
    ageAuestion = "How old r u?"
    print(ageAuestion)
    age = input()
    print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
    answer = input()
    if answer == "Yes":
        print("Name: " + name + ", " + "Age: " + age)
        break
    else:
        print("Error")
        continue

While Doc

Younes
  • 391
  • 2
  • 9
1

use while True and put a check when you want to break out of while loop.

while True:
    `your code`
      .
      .
     `condition to break out of while loop`
gsb22
  • 2,112
  • 2
  • 10
  • 25
1

Use a while loop:

answer = ''

while answer != 'yes':
    nameQuestion = "Whats ur name?"
    print(nameQuestion)
    name = input()
    ageAuestion = "How old r u?"
    print(ageAuestion)
    age = input()
    print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
    answer = input()

print("Name: " + name + ", " + "Age: " + age)
Wouter
  • 190
  • 2
  • 8
1
while True:
  nameQuestion = "Whats ur name?"
  print(nameQuestion)
  name = input()
  ageAuestion = "How old r u?"
  print(ageAuestion)
  age = input()
  print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
  answer = input()
  answer = answer.lower()
  if answer == "yes":
      print("Name: " + name + ", " + "Age: " + age)
      break
  else:
    pass

You can use while loop

Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
1

You can do something like this:

wrong_data = True
while wrong_data:
    name = input("Whats ur name?")
    age = input("How old r u?")
    answer = input(f"Your name is {name}  and your age is {age}?(yes or no)")
    # you should put answer.lower so both 'yes' and 'Yes' are interpreted correctly
    if answer.lower() == "yes":
        print(f"Name: {name}, Age: {age}")
        wrong_data = False
Morgana
  • 205
  • 3
  • 5