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)
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)
+ 2
Because the while loop is not stopping...
Its continuing infinitely...
You need to stop that to get output
+ 1
James Murphy happy coding đ
+ 1
How about this one? :-
print(sum(100 * (int(input()) > 2) for i in range(5)))
# Hope this helps
# Happy coding!
+ 1
Thanks very much, this has been driving me mad.
0
Thank you, I'll give that a whirl.
0
James Murphy Would you mind posting the question here? Thank you.
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
0
I've updated my code but now am getting 600 (at least I have an output) but should get 200.
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)
0
You need to indent statements under loop
- 1
You need to use 'break' statement