+ 1

How to write a code that count the number of inputs and times it with a fixed number? Thank you!

Question: 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. My Answer: age = int(input()) while True: if age <= 3: continue if age > 3: print (100)

27th Feb 2022, 7:08 AM
xy._.
xy._. - avatar
6 Respuestas
+ 1
ages = [] total = 0 i = 0 while i < 5: x = int(input()) ages.append(x) i += 1 for age in ages: if age >= 3: total += 100 print(total) Note : you should write the five inputs in seperate line like this 20 24 23 26 2
27th Feb 2022, 9:15 AM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
“total = 0 for _ in range(5): x = int(input()) if x >= 3: total += 100 print(total)” For this code that you wrote, what does the _ represent? Is "range" a command word? How does this code repeat five times without using the while loop? Thank you!
27th Feb 2022, 10:11 PM
xy._.
xy._. - avatar
+ 1
_ is not default u can use any character instead of it like i or num, ...etc but not any Python keywords range() is a built in method and it's makes the code loop 5 times I advise u to search 🔍🔎 about range() method and you will understand the code
27th Feb 2022, 11:13 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 1
Okkkk thank you very much!
28th Feb 2022, 8:35 AM
xy._.
xy._. - avatar
0
I did this to manage me to loop on the five ages instead of using 5 variables ravilnicki
27th Feb 2022, 9:26 AM
Muhammad Galhoum
Muhammad Galhoum - avatar
0
Nice 👍🙂 ravilnicki
27th Feb 2022, 9:39 AM
Muhammad Galhoum
Muhammad Galhoum - avatar