Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Calculator Help
#1
I just started learning python 3 this week and I'm trying to a code a simple calculator. I'm a straight beginner and have a good passion to learn python.

What I'm trying to do is have my code throw out an error to user if they do not enter in a valid number.
Example: if user enters in a 5 then the program does nothing and continues to the next line, but if the user enters in a letter then the program will tell the user "Hey that's not a valid number!".

Here is my code:
-Currently the program spits out an error to user if they enter in something other than a valid math operation.
-Desired is to have the program spit out another error to the user if they don't enter in a proper integer/float


#Simple Calculator v1.0
print("::: Welcome To My Calculator :::")
print("::: Welcome To My Calculator :::")
print("::: Welcome To My Calculator :::\n")
print("Math Operations")
print("       +\n" "       -\n" "       *\n" "       /\n")

num1 = float(input("Enter First Number: "))
op = input("Enter Operation: ")
num2 = float(input("Enter Second Number: "))

try:
    val = int(num1)
except ValueError:
    print("Error: Enter A Valid Number")

try:
    val = int(num2)
except ValueError:
    print("Error: Enter A Valid Number")

if op == "+":
    print(num1 + num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1 * num2)
elif op == "/":
    print(num1 / num2)
else:
    print("Not a valid math problem")
Console Error:

Enter First Number: 54
Enter Operation: -
Enter Second Number: Hi
Traceback (most recent call last):
  File "C:/Users/Xerxes/PycharmProjects/New Projects/Calculator.py", line 10, in <module>
    num2 = float(input("Enter Second Number: "))
ValueError: could not convert string to float: 'Hi'

Process finished with exit code 1
Reply
#2
You are converting to float on lines 8 and 10, but you aren't trying to catch errors until lines 12-20. If you want to catch the errors being raised, you need to either move your try blocks back to where the float(input(...)) calls are, or you need to wait to convert to float until the try block.

You are going to have problems later on, though. You are just printing a warning if an error is raised, you are not actually dealing with the problem. In your example, you may print a warning that the second number is invalid, but then your program is still going to subtract Hi from 54. You probably want to put everything into a while loop:

while True:
    try:
        num1 = float(input('Enter second number: '))
        break
    except ValueError:
        print('Please enter a valid number.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple tip calculator DragonG 5 5,367 Oct-23-2018, 04:54 AM
Last Post: snippsat
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,151 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Need help with simple calculator. ghost0fkarma 3 2,743 Sep-11-2018, 07:40 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020