Else-statement. I couldn't find the bug in my code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Else-statement. I couldn't find the bug in my code

Hi guys! I'm struggling with this problem "Too young for riding" using the else-statement in Python. My code works in all the Test cases, except the Test #4 (which is hidden, so I couldn't investigate why my code is not working). Does anyone see a bug in my code below?: ages = [] i = 0 while i<3: rider_age = int(input()) ages.append(rider_age) i+=1 if rider_age <17: print("Too young!") break else: print("Get ready!") break

24th Feb 2021, 3:21 PM
Selva Ramírez Bracho
Selva Ramírez Bracho - avatar
6 Answers
+ 3
Can you please share more description to your task?
24th Feb 2021, 3:48 PM
Shivani 📚✍
Shivani 📚✍ - avatar
+ 2
by doing your else statement inside the loop, you're necessarly breaking it after the first input... also, you doesn't need to store input: i = 0 while i<3: rider_age = int(input()) i+=1 if rider_age <17: print("Too young!") break else: print("Get ready!") else statements are valid after a loop: it's executed in case of loop break ;)
24th Feb 2021, 4:44 PM
visph
visph - avatar
+ 1
hi! if this is for sololearn the while loop won’t work but if its for a regular python console, you can try changing the else statement to elif i==2 because the break statement in the else statement means the program will only check the first variable.
24th Feb 2021, 4:01 PM
madeline
madeline - avatar
+ 1
I would like to recommend solution such as: ages = [] i = 0 y = 1 while i<3: rider_age = int(input()) if rider_age <16: y = 0 break else: ages.append(rider_age) i+=1 #print(ages) if y>0: print("Get ready!") else: print("Too young!")
24th Feb 2021, 5:01 PM
JaScript
JaScript - avatar
0
Hi Shivani 📚✍! This is the task: The carousel is designed for 3 people who are each at least 16 years old. You are given a program that takes all 3 passengers' ages as inputs and inserts them in a list. Complete the program so that if it finds a value less than 16, it breaks the loop and outputs "Too young!". If the age requirement is satisfied, the program outputs "Get ready!". Sample Input 18 26 19 Sample Output Get ready!
24th Feb 2021, 3:52 PM
Selva Ramírez Bracho
Selva Ramírez Bracho - avatar
0
Thank you very much, guys! Your comments have been super useful to me
24th Feb 2021, 5:20 PM
Selva Ramírez Bracho
Selva Ramírez Bracho - avatar