Home > Blockchain >  HW Help on loops and error expections and 'done'
HW Help on loops and error expections and 'done'

Time:02-24

the conditions are: Repeatedly reads numbers until the user enters 'done'. Once 'done' is entered, break out of the loop. If the user enters anything but a number, capture the error using a try-except construct and display the message "Bad Data" Once 'done' is entered print out one of 2 messages: If the total is greater or equal to 200 then: "The total (the value in the total variable) for the numbers entered is Large" If the total is less than 200 then: "The total (the value in the total variable) for the numbers entered is Small"

count = 0

while count <= 200:

    x = int(input('Please enter a number to add: '))
    count = count   x

    if x >= 200:
        print('The total', count, 'for the number entered is Large')
        break

    elif x == 'done':
        print ('The total',count,'for the number entered is Small')
        break

    elif x == ValueError:
        print('Bad Data')
        continue

Im a beginner so a little help would be cool.

CodePudding user response:

I have edited your code a little bit. It might help you to understand the logic.

total = 0
while True:
    userInput = input('Please enter a number to add: ')
    if userInput == "done":
      break
    try:
      x = int(userInput)
      total  = x
      if total >= 200:
          print('The total', total, 'for the number entered is Large')
      elif total <=  200:
          print ('The total',total,'for the number entered is Small')
    except:
      print("Bad Data")

A few notes. I have changed the count name to total since the logic of your problem seems to be closer to this name.

Output

Please enter a number to add: I am inputting bad data
Bad Data
Please enter a number to add: 100
The total 100 for the number entered is Small
Please enter a number to add: 0
The total 100 for the number entered is Small
Please enter a number to add: 50
The total 150 for the number entered is Small
Please enter a number to add: 30
The total 180 for the number entered is Small
Please enter a number to add: -50
The total 130 for the number entered is Small
Please enter a number to add: done

CodePudding user response:

When you accept the input value you need to check if it is a number before you try to convert it to an int. So you have two ways of doing this: either wrap the first line in a try except block to catch the value error or just check to see if the input they supply is actually a number. Also as Tim Roberts said above you can't check for "done" if you have already converted to an integer so that's why I have moved that check to the top of each example.

Version 1 (Try Except):

count = 0

while count <= 200:
     user_input = input('Please enter a number to add: ')
     if user_input.lower() == 'done':
        print('The total', count, 'for the number entered is Small')
        break
     try:
         x = int(user_input)
     except ValueError:
         print('Bad Data')
         continue

     count = count   x

     if x >= 200:
         print('The total', count, 'for the number entered is Large')
         break

Version 2 (Check if number):

count = 0

while count <= 200:
     user_input = input('Please enter a number to add: ')

     if user_input.lower() == 'done':
         print('The total', count, 'for the number entered is Small')
         break

     if user_input.isnumeric():
         x = int(user_input)
     else:
         print('Bad Data')
         continue

     count = count   x

     if x >= 200:
         print('The total', count, 'for the number entered is Large')
         break

PS. Adding the .lower() to the user_input allows them to type 'done' or 'DONE' or any combination of uppercase and lowercase and it will still exit.

  • Related