Test if a string represents a number

  calculator, if-statement, list, python, python-3.x

Building a simple calculator for my first Python project and encountered a question shown in the comments below:

special_keys = ["`", "~", "!", "@", " ", "#", "$"]

while True:
    num1 = input("Enter First Number: ")
    if num1.isalpha():
        print("Invalid, Try Again.")
        continue

     # elif num1 contains an element or part of an
     # element in special_keys do the following:
         # print("Invalid, Try Again.")
         # continue

    else:
    num1 = float(num1)
    break

Source: Python-3x Questions

LEAVE A COMMENT