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
You just need nested if
condition
total = 0
print("Enter the values of amounts collected")
while total<200: # Loop in until total < 200
new_number = int(input('> '))
if new_number in [10,20,50]: # First check input number is in 10,20,50
total = total + new_number # Then add sum to total
if total == 200: # If total = 200 break
print("You have successfully collected 200")
break
elif total >200: # If total > 200 break
print("Amount collected exceeds 200")
break
else: # If number not in 10,20,50 then print invalid input
print("Invalid input")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.