Home > Back-end >  Can I incorporate if, else statements with input?
Can I incorporate if, else statements with input?

Time:01-11

I am practicing and learning Python, and I had this idea to incorporate if, else statements with input, but it didn't work. So, I tried break, continue and except commands.

name = input("Hello! What's your name?" )
print("Hello,"   name   "!")
age = int(input("How old are you? "))
while age <= 13 or age >= 55:
    if age < 13:
        print("You are too young!")
        continue  
    if age >= 55:
        print("You are too old!")
        continue
    else:
        print("Welcome!")
        continue
why = input("Why are you here? ")
print("Welcome!")

So, I was trying to go when a person who is over 55 or under 13, it'll boot them out of the input. But, when I tried it, it just printed the print on and on, infinitely.

name = input("Hello! What's your name?" )
print("Hello,"   name   "!")
age = int(input("How old are you? "))
while age <= 13 or age >= 55:
    if age < 13:
        print("You are too young!")
        break  
     if age >= 55:
        print("You are too old!")
        break
    else:
        print("Welcome!")
        continue
why = input("Why are you here? ")
print("Welcome!")

Here is another one, I tried tweaking it. But what happened is I tried doing break. It did the print but just moved on with the command.

Can someone give me a explanation on why this is happening and how to fix it?

CodePudding user response:

I can spot two main issues

  1. You should replace the second if with an elif statement
  2. The last two lines aren't indented

For the first problem, both if statements will likely run

age = 12
if age < 13: # This is true
    print("You are too young!")
    break  
if age >= 55: # This is false
    print("You are too old!")
    break
else: # But this will also run
    print("Welcome!")
    continue

You can fix this by replacing it with an elif

age = 12
if age < 13: # This is true
    print("You are too young!")
    break  
elif age >= 55: # This won't run
    print("You are too old!")
    break
else: # This also won't run
    print("Welcome!")
    continue

(I should note that, because of the break statements in each if, the code never reaches the second if statement.)

The second issue comes from the fact that Python puts a large emphasis on tabs. For example,

while True:
    print("Hello")
print("World!") # Un-indented code means it's not part of the `while` loop

Will only print Hello over and over again. Meanwhile,

while True:
    print("Hello")
    print("World!")

Will print Hello World! forever. Indenting the last two lines by a single tab will put the statements into the while loop, which should fix your problem. Hope this helps!

CodePudding user response:

Explanation of continue and break

So continue is used in loops and will go to the next iteration of the loop (skipping over any other instructions that you have below the continue keyword). Here you are using a while loop, as you don't have any condition that updates the value of age the loop will continue to repeat (infinite) because there is no update to make the condition (age <= 13 or age >= 55) false to terminate the loop.

break on the other hand is used to stop further iterations in a loop.

suggestions

your while loop is necessary because you are only ever checking the age value once.

would suggest using @Steggy's recommendation

if age < 13: # This is true
    print("You are too young!")
    break  
elif age >= 55: # This won't run
    print("You are too old!")
    break
else: # This also won't run
    print("Welcome!")
    continue

CodePudding user response:

debugging your 2nd code.

User input age value less than 13: It will print the statement too young and break statement will help to exit the while loop

User input age value equal to 13: first and second if block not satisfy the condition hence it will run else block it will print welcome and by doing continue you move to the next iteration of while loop. hence again and again this else block run and your while loop tend to infinity reason your age value is fixed. solution you can take the age input inside the else block so that user input again or you can use break as you used in first two if's

User input age value greater than or equal to 55: It will run second if block and break statement helps to terminate while loop.

Also if your age is fixed i don't think the while loop is necessary you can directly do if elif..

If your age is not fixed. than you should take input inside the while loop.

while True:
    # This will take input until age input is less than 13 or age is greater than or equal to 55.
    age = int(input("How old are you? ")) 
    if age < 13:
        print("You are too young!")
        break  
    elif age >= 55:
        print("You are too old!")
        break
  • Related