0
from math import *
print("Calculator that performs simple arithmetic with 2 variables.\n")

def Input():
  global Arithmetic_Operator
  print("0: Exit\n1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\n5: Square root\n")
  print("Only numbers 0 to 5 are accepted.")
  Arithmetic_Operator = input("Which arithmetic operator do you want to use? ")
  if Arithmetic_Operator == "0":
    print("Exiting...")
    exit()
  elif Arithmetic_Operator == "1":
    global Arithmetic_Operator_Addition
    print("\n")
    Arithmetic_Operator_Addition = "Addition"
    Options()
  elif Arithmetic_Operator == "2":
    global Arithmetic_Operator_Subtraction
    print("\n")
    Arithmetic_Operator_Subtraction = "Subtraction"
    Options()
  elif Arithmetic_Operator == "3":
    global Arithmetic_Operator_Multiplication
    print("\n")
    Arithmetic_Operator_Multiplication = "Multiplication"
    Options()
  elif Arithmetic_Operator == "4":
    global Arithmetic_Operator_Division
    print("\n")
    Arithmetic_Operator_Division = "Division"
    Options()
  elif Arithmetic_Operator == "5":
    global Arithmetic_Operator_Square_root
    print("\n")
    Arithmetic_Operator_Square_root = "Square root"
    Options()
  else:
    print("Invalid input. Please try again.\n")
    Input()

def Options():
  if Arithmetic_Operator == "1":
    print("Operator chosen: " + Arithmetic_Operator_Addition)
    First_Number = float(input("First number: "))
    Second_Number = float(input("Second number: "))
    Answer = str(First_Number + Second_Number)
    print("Answer: " + Answer + "\n")
    Input()
  elif Arithmetic_Operator == "2":
    print("Operator chosen: " + Arithmetic_Operator_Subtraction)
    First_Number = float(input("First number: "))
    Second_Number = float(input("Second number: "))
    Answer = str(First_Number - Second_Number)
    print("Answer: " + Answer + "\n")
    Input()
  elif Arithmetic_Operator == "3":
    print("Operator chosen: " + Arithmetic_Operator_Multiplication)
    First_Number = float(input("First number: "))
    Second_Number = float(input("Second number: "))
    Answer = str(First_Number * Second_Number)
    print("Answer: " + Answer + "\n")
    Input()
  elif Arithmetic_Operator == "4":
    print("Operator chosen: " + Arithmetic_Operator_Division)
    First_Number = float(input("First number: "))
    Second_Number = float(input("Second number: "))
    Answer = str(First_Number / Second_Number)
    print("Answer: " + Answer + "\n")
    Input()
  elif Arithmetic_Operator == "5":
    print("Operator chosen: " + Arithmetic_Operator_Square_root)
    Number = float(input("Number: "))
    Answer = sqrt(Number)
    print("Answer: " + str(Answer) + "\n")
    Input()
Input()

In the Input() module, all my input fields are designed in a way that only numbers 0 to 5 are accepted and other inputs are rejected. This way, I can prevent abuse of the input fields in the Input() module. However, in the Options() module, the input fields are susceptible to abuse if the user entered an alphabet or other symbols, due to the use of the float() function? Is there a way to prevent the script from crashing when such alphabets and symbols are entered in the Options() module?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    Does this answer your question? [Checking if a string can be converted to float in Python](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python) – Joe Mar 13 '22 at 08:30

1 Answers1

2

What you are looking can be done with exception handling

try:
  First_Number = float(input("First number: "))
except ValueError as e:
  print("Only numbers allowed")

I hope your homework also touches refactoring at some point in the future

gnight
  • 429
  • 2
  • 10