+ 2

User updated variables?

I’m looking for advice. My goal is to have the (imaginary) user create the value of the variable. Then the loop will continue looping by that value until it hits the break condition. total = 0 tickets = int(input()) passengers = tickets while (True): age = int(input()) if age > 3: total += 100 passengers -= 1 else: passengers -= 1 if passengers == 0: break print(total)

23rd Mar 2022, 3:01 PM
Eric
Eric - avatar
7 Antworten
+ 3
ah sorry. So basically the idea is user enters something like 5 for tickets that value is added to passengers as it’s value then the code checks the age of each passenger if anyone is over 3 years old, it will add 100 to the Total and updates the pasengers variable. if they are under, it just updates the passengers variable. once passengers updates to 0, have the sequence break and print the total cost.
23rd Mar 2022, 3:19 PM
Eric
Eric - avatar
+ 2
Thank you! So I was close…….
23rd Mar 2022, 3:29 PM
Eric
Eric - avatar
+ 2
Eric , have a look at the file. it is based on your code, but modified slightly. see my comments https://code.sololearn.com/cZym78r3YMor/?ref=app
23rd Mar 2022, 5:19 PM
Lothar
Lothar - avatar
+ 1
Don't understand it clearly.. In this code, if the user enters value all greater than age>3 then it never enter to else break. take it out of else block. a bit more clarity by input , output expected?
23rd Mar 2022, 3:04 PM
Jayakrishna 🇮🇳
+ 1
total = 0 tickets = int(input()) passengers = tickets while (True): age = int(input()) #if age >= 3 add else ingores if age >= 3: total += 100 passengers -= 1 #update if passengers == 0: #check break print(total) #if it means to consider above 3 then use age>3 . #you can directly use ticket variable or use passenger= int ( input() ) also as a condition for while.... #hope it helps...
23rd Mar 2022, 3:27 PM
Jayakrishna 🇮🇳
+ 1
Lothar that’s a very intersting approach. Thank you. It’s always great to see how other people approach the same problem. I appreciate you taking the time to reply.
24th Mar 2022, 2:54 AM
Eric
Eric - avatar
0
Yes. There is else part extra code. Stil works same if you remove that. And identation is important in python. Break clouse should be added outside. You're welcome..
23rd Mar 2022, 3:32 PM
Jayakrishna 🇮🇳