+ 4
total = 0 #your code goes here i=0 while i<5: age =int(input()) if age < 3: continue total+= 100 print (total )
Whats my mistake in this code Problem is that You are making a ticketing system. The price of a single ticket is $100. For children under 3 years old, 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 ANSWEW Sample Output 400 ANSWERED Bcuz of just a few spaces in the code so that didnt work .d so disgusting
8 Answers
+ 4
Nuri ,
Your "while" is infinity loop, you must increase "I" counter.
+ 2
Post the code using the code playground feature
+ 1
There are a number of reasons why this isn't working.
One thing is you're using a "while loop" and it has no loop control.
'i" is always going to be equal to 0 because you have nothing to increment it.
If you're going to use a while loop, a good practice is to create an exit for it.
However, since you stated that there is a predetermined number of people (5 passengers), you should be using a "for loop" with a range of 5.
You should start off by creating a for loop.
+ 1
for i in range(5):
This "for loop" will loop a total of five times. Since you're only checking for five people, that's all you need.
Next, you can place your input variable at the start of the for loop so that it gets checked first.
age = int(input("Input age."))
Once that gets passed through, you want to create an if statement to check the condition.
if age > 3:
Here, you're checking to see if the Age entered is greater than 3. If you want to include the age of 3, then you need to use a >= comparison.
total += 100
This will only occur if the conditions of the if statement are True.
The way you had it with "continue" wasn't going to work because after stuff like "continue" and "return", it doesn't read anything else in the block.
print(total)
This last statement will give you your output.
+ 1
Basically, use "while loops" if you want to create something that is indefinite. Something as in "you don't know how many passengers" in your case. Also things like whether or not a player wants to continue playing a game.
If you do use while loops, immediately create your exit condition to break out of it just in case you forget. Write your code in between, and then update the exit condition after you edit whatever code inside.
Counters are usually great because it limits the number of times it can loop at a maximum.
Otherwise, create a Boolean condition that is checked at the end of the loop.
Something like:
playing_game = True
while playing_game:
# Your code here.
game_over = input("End?")
if game_over == "yes":
playing_game = False
Basically, the point is to create conditions to exit your while loop, and only use them if you have undetermined numbers of usages for the program.
+ 1
Adrian really really tysm !! Ur comments are so helpfull
+ 1
Nuri no problem at all. Glad to be able to help.
0
its says line 5 , E0F error .. but there is not