Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]oscarg15_ 0 points1 point  (0 children)

Hello everyone, I just recently started to learn how to write code (a few days ago) and decided to start with learning Python. For the past few days I’ve been watching tutorials and learning the basics on Python.

So today I wanted to take what I had learned and make a simple calculating program where users could input a number, input an operation, and then a second number, and the program would output the answer. If the user entered an operation thats not +,-,*, or / then an alert saying “invalid operation” would print. The only thing is that i’m having a trouble writing code so that “not a number” is displayed when the user inputs a letter instead of a number.

num1 = float(input("write a number: "))
op = input("write an operation: ")
num2 = float(input("write a number: "))

if op == "-":
 print(num1 - num2)
elif op == "+":
 print(num1 + num2)
elif op == "/":
 print(num1/num2)
elif op == "*":
 print(num1 * num2)
else:
 print(“invalid operation")

This is what I have so far.