1

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!

asked Jan 11, 2018 at 3:25
1
  • 1
    If you're asking about an error, it's tremendously helpful to include the exact error message in your question. Better yet, make sure that line numbers in the error match what you've posted in your question. Commented Jan 11, 2018 at 3:29

2 Answers 2

1

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)
answered Jan 11, 2018 at 3:47
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm, thank you. Because I haven’t gotten to the sys module, I just replaced sys.exit(1) with a break. Thank you for the response.
You are welcome please pick my answer and call it a night ;) @captainkidd5
1

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).

answered Jan 11, 2018 at 3:31

1 Comment

oh I see. I didn't realize the first two lines were asking for the input right then and there. I thought I was just defining the variables there, to be used later on. Thank you for the thoughtful reply!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.