0

I'm very new to python, just started learning, and wanted to test my skills by building a simple arithmetic calculator. I've tied up most loose ends in my code, but don't know how to provide an error message and new input line to the user when they place a letter/word in the input line for numbers. Again, I'm very new to python and have no idea how to fix this. Also curious about how ugly my code looks. Any advice is appreciated, thanks.

print("Welcome to the Basic Python Calculator!")
def calculator():
  num1 = input("Name your first number: ")
  num2 = input("Name your second number: ")
  operator = input("Name an operation. Add, Subtract, Multiply, or Divide. (Match Capitalization)\n")
  while operator != "Add" and operator != "Subtract" and operator != "Multiply" and operator != "Divide":
      operator = input("Name an operation. Add, Subtract, Multiply, or Divide. (Match Capitalization)\n")
  if operator == "Add":
    result = float(num1) + float(num2)
    print(num1 + " + " + num2 + " = " + str(result))
  elif operator == "Subtract":
    result = float(num1) - float(num2)
    print(num1 + " - " + num2 + " = " + str(result))
  elif operator == "Multiply":
    result = float(num1) * float(num2)
    print(num1 + " ● " + num2 + " = " + str(result))
  elif operator == "Divide":
    result = float(num1) / float(num2)
    print(num1 + "/" + num2 + " = " + str(result))

calculator()
 
xfmw
  • 53
  • 1
  • 6

2 Answers2

2

You can do this by using a try/catch block along with a while loop. So when you want to take number input, do this

while true: 
   user_input = input("Enter a number")
   try:
      user_input_number = int(user_input)
      break
   except:
      print("ERROR")
print(user_input_number)
0

You can simply use isnumeric() (i.e num1.isnumeric(), num2.isnumeric()) to see if you have a number or not. and using that you'll know how to deal with the input. You won't need a try catch, although having one would be a good idea.

Side note for floats: for float strings, one quick way could be to remove . and check the resulting string with isnumeric to know if you are dealing with numbers

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • Why would you go through this if you end up recommending a `try` / `except` anyway? – tripleee Jul 28 '20 at 04:47
  • That is exceptionally useful. Thank you for the info. Like I said before, I'm very new to python, so having a simple function to use in conjunction with the little I know is very helpful. Any other tips or simple functions you'd recommend to a beginner? – xfmw Jul 28 '20 at 05:04
  • @B0bby No problem. I dont know what you have in mind , but basically its good to start simple, and then make it more complex. try new language constructs and see how they benifit you. read more about try except (or in other languages try-catch) and try to have them implemented in your experiments. Good luck – Hossein Jul 28 '20 at 05:27
  • @tripleee there are many options to do this, this can be specifically useful for a beginner to learn more about what can he do and why something maybe a better choice. – Hossein Jul 28 '20 at 05:29