0

Why am I getting no output for this code?

I'm stuck finding the total of 5 tickets age = int(input()) b = 0 total = 0 while True: b <=5 b+=1 if age > 2: total = total + 100 print(total)

25th Aug 2021, 8:04 AM
James Murphy
James Murphy - avatar
12 Answers
+ 3
Hi James! That's because your code doesn't check all inputs since you declared input variable outside loop. So, it always checks first input and ignores the rest. Also, b should start from 1 for taking 5 inputs. Here it is your working code b = 1 total = 0 while b <=5: age = int(input()) b+=1 if age > 2: total = total + 100 if age>=2: continue print(total)
25th Aug 2021, 8:48 AM
Python Learner
Python Learner - avatar
+ 2
Because the while loop is not stopping... Its continuing infinitely... You need to stop that to get output
25th Aug 2021, 8:07 AM
Anas Dharar
Anas Dharar - avatar
+ 1
James Murphy happy coding 😄
25th Aug 2021, 8:14 AM
Anas Dharar
Anas Dharar - avatar
+ 1
How about this one? :- print(sum(100 * (int(input()) > 2) for i in range(5))) # Hope this helps # Happy coding!
25th Aug 2021, 8:37 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Thanks very much, this has been driving me mad.
25th Aug 2021, 9:01 AM
James Murphy
James Murphy - avatar
0
Thank you, I'll give that a whirl.
25th Aug 2021, 8:11 AM
James Murphy
James Murphy - avatar
0
James Murphy Would you mind posting the question here? Thank you.
25th Aug 2021, 8:20 AM
Calvin Thomas
Calvin Thomas - avatar
0
You are making a ticketing system. The price of a single ticket is $100. For children under 3 years of age, the ticket is free. Your program needs to take the ages of 5 passengers as input and output the total price for their tickets. Sample Input 18 24 2 5 42 Sample Output 400
25th Aug 2021, 8:26 AM
James Murphy
James Murphy - avatar
0
I've updated my code but now am getting 600 (at least I have an output) but should get 200.
25th Aug 2021, 8:27 AM
James Murphy
James Murphy - avatar
0
age = int(input()) b = 0 total = 0 while b <=5: b+=1 if age > 2: total = total + 100 if age>=2: continue print(total)
25th Aug 2021, 8:27 AM
James Murphy
James Murphy - avatar
0
You need to indent statements under loop
26th Aug 2021, 4:57 AM
Thirunalankumar
Thirunalankumar - avatar
- 1
You need to use 'break' statement
25th Aug 2021, 8:48 AM
Person
Person - avatar