-1

i want to add loop inside my code like if the input was no in (maty) the code will say try again and it will loop to Step on mat

mat = input("Step on mat : ")

maty = input("Is your weight between 40 and 50 ? : ")

if maty == ("yes"):
    h = input("Select your height on the scale : ")
    j = input("Is "+ h +" your height ? : ")
    if j == ("yes"):
        print("You are ready to go !")
    else:
        print("Try again")

if maty == ("no"):
    print("try again")  

im a beginner in python

thanks

Saad
  • 1
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Pranav Hosangadi May 20 '21 at 17:16

3 Answers3

2

Answer:

Below i have used a while loop that will run until one will give input as "yes". and also to perform a case-insensitive comparison of user input("yes"/"no") , i have modified your code as maty.lower().

maty = "no"
while maty.lower() == "no":
  maty = input("Step on mat :")
  maty = input("Is your weight between 40 & 50 ? : ")
  if maty.lower() == "yes":
    h = input("Select your height on the scale :")
    j = input("IS “+h +” your height ? :")
    if  j == "yes":
        print("You are ready to go! ")
    else:
        print("Try again")
        if maty == "no":
            print("try again")
0

A while loop should do the job:

while maty == "no":
    mat = input("Step on mat : ")

    maty = input("Is your weight between 40 and 50 ? : ")

    if maty == ("yes"):
        h = input("Select your height on the scale : ")
        j = input("Is "+ h +" your height ? : ")
        if j == ("yes"):
            print("You are ready to go !")
        else:
            print("Try again")

    if maty == ("no"):
        print("try again")  
Riccardo Lamera
  • 105
  • 1
  • 13
0

You can use a while loop.

mat = 'no'
tries = 0
while (mat != 'yes') and (tries < 5):
   mat = input("Step on mat : ")
   if mat != yes:
      print('Please answer "yes" to continue')
      tries += 1
      continue
   elif tries == 5:
      print('Please restart program and try again')
   maty = input("Is your weight between 40 and 50 ? : ")

   if maty == ("yes"):
       h = input("Select your height on the scale : ")
       j = input("Is "+ h +" your height ? : ")
       if j == ("yes"):
           print("You are ready to go !")
       else:
           print("Try again")