Too Young Too Drive - Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Too Young Too Drive - Python

I’m having some trouble with this Code Coach. I think something might be wrong with the else statement. What do I fix it? i = 0 while i<3: rider_age = int(input()) i+=1 if rider_age <17: print("Too young!") break else: print("Get ready!")

24th Nov 2022, 4:38 AM
Lashana Jegatheswaran
4 Answers
+ 3
If you want "Get ready!" to print only once (which actually makes more sense, my mistake for assuming otherwise), you can readjust the indentation of the 'else' to extend from 'while' instead of 'if': i = 0 while i<3: rider_age = int(input()) i+=1 if rider_age <17: print("Too young!") break else: print("Get ready!") This way, the if statement still belongs inside the while loop. Here the else clause will only be executed if the loop exits normally (without break).
24th Nov 2022, 9:12 AM
Mozzy
Mozzy - avatar
+ 2
Indentation is important in python. The statements that you wish to run within the while loop must be indented by 1 level for it to be part of the loop. Same goes for the if and else statements. Try this: i = 0 while i<3: rider_age = int(input()) i+=1 if rider_age <17: print("Too young!") break else: print("Get ready!")
24th Nov 2022, 6:02 AM
Mozzy
Mozzy - avatar
0
The code prints “Get Ready!” three times. How do I change the code to make it print the phrase one time only?
24th Nov 2022, 6:40 AM
Lashana Jegatheswaran
0
Thank you so much!
24th Nov 2022, 12:10 PM
Lashana Jegatheswaran