+ 2

How to write a program that would keep taking inputs from the user until the sum of all the inputs reach 200,

The numbers permissible for the user input are: 10, 20 and 50. If any other number is entered, then the program should declare it as invalid. I've tried the following but it doesn't seem to work out: count = 0 total = 0 print("Enter the values of amounts collected") while True: new_number = input('> ') count = count + 1 total = total + int(new_number) if total==200 : print("You have successfully collected 200") break if total>200: print("Amount collected exceeds 200") break Sample input: > 10 > 50 > 50 > 50 > 10 > 20 > 10 Sample output: You have successfully collected 200 Sample input: > 190 ... Sample output: Invalid input Sample input: > 50 > 50 > 50 > 20 > 50 Sample output: Amount collected exceeds 200

9th May 2021, 2:58 PM
MsBonnie
MsBonnie - avatar
4 Respuestas
11th May 2021, 3:42 AM
CouldntBeBothered.py
CouldntBeBothered.py - avatar
+ 1
Use a while loop and exit when the sum >= 200
11th May 2021, 11:30 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
MsBonnie Here is a solution of your problem but what if amount is less than 200? https://code.sololearn.com/cajiW4FSyOFs/?ref=app
9th May 2021, 4:05 PM
A͢J
A͢J - avatar
- 1
MsBonnie Why you are passing '> ' in input function. No need to do that. Just do this new_number = input() To handle invalid input just use try except block
9th May 2021, 3:10 PM
A͢J
A͢J - avatar