Kidd
Kidd

Reputation: 97

Calculator exercise in Python

this is my first post so PLEASE let me know if I'm not doing this correctly! Also, please don't roast me too hard for my code, I'm (very) new!

I'm trying to create a basic 'calculator' that takes two numbers as input and spits the summation back at the user. If the user types 'quit', I want the program to break, and if they enter a string rather than an integer I want the program to respond telling them to enter a number. I'd also like the program to continue after the numbers are added so that the user can continue to add numbers until they choose to type 'quit'.

My problem is this:

The program runs, and asks the user for the first and second numbers, however, if the user types in a string, it STILL produces a traceback error. I'm assuming I didn't type the exception properly somehow. Also, the loop never ends so it returns a constant string of the two numbers entered.

number1 = int(input("Enter a number: "))
number2 = int(input("Enter another number: "))
error_msg = print("That isn't a number. Please enter a number")

flag = True

while flag == True:
    try:
        print(number1)
    except ValueError:
        print(error_msg)
    try:
        print(number2)
    except ValueError:
        print(error_msg)
    summation = number1 + number2
    print(summation)

    if number1 or number2 == 'quit':
        flag == False

Here's my error message:

Enter a number: 3
Enter another number: f
Traceback (most recent call last):
  File "errors.py", line 2, in <module>
    number2 = int(input("Enter another number: "))
ValueError: invalid literal for int() with base 10: 'f'

EDIT - The error message, Thank you, larsks.

Any help would be greatly appreciated!

Upvotes: 1

Views: 520

Answers (2)

Mike Tung
Mike Tung

Reputation: 4821

You almost got it but your inputs are in the wrong place, here's a more pythonic approach.

import sys

flag = True
while flag:
    number1 = input("Enter a number: ")
    if number1 == 'quit':
      sys.exit(1)

    number2 = input("Enter another number: ")
    if number2 == 'quit':
      sys.exit(1)

    try:
        number1 = int(number1)
        number2 = int(number2)
    except (ValueError, AttributeError):
        print("That isn't a number. Please enter a number")
    else:
      summation = number1 + number2
      print(summation)

Upvotes: 1

larsks
larsks

Reputation: 311268

You are asking for input there:

number1 = int(input("Enter a number: "))
number2 = int(input("Enter another number: "))

Then you are, at the same time, converting the user input to an integer using int(...). If you type in a non-integer value, you get the exception:

Traceback (most recent call last):
  File "calc.py", line 1, in <module>
    number1 = int(input("Enter a number: "))
ValueError: invalid literal for int() with base 10: 'hello'

There is no try/except block around these lines, so the traceback causes the program to exit.

You have some try/except blocks later on in your code, but they don't do anything:

try:
    print(number1)
except ValueError:
    print(error_msg)

print(number1) is never going to raise a ValueError exception (because print doesn't care if you give it a number or a string or something else).

Upvotes: 1

Related Questions