Simple Calculator to add/subtract/multiply/divide 2 inputs This is for an assignment in my robotics class so the requirements I needed to meet was that it had to be in a while loop and the input numbers needed to be float point. I just want to make sure that I'm not using any bad practices and if there are any areas that can be improved.
while (True):
    # Gets operator, converts to int and breaks if its not a number
    try: operator = int(input(&quot;Choose an Operator:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\nInput (1/2/3/4):&quot;))
    except:
        print('Invalid Input: Choose A Valid Operation')
        break
    
    # Checks the operator is within 1 and 4
    if operator &gt; 4 or operator &lt; 1: 
        print('Invalid Input: Choose A Valid Operation')
        break
    
    # Gets first and second number, converts numbers to float and breaks if its not a number
    try: firstnum = float(input('First Number:'))
    except: 
        print('Invalid Input: Not a valid number')
        break
    try: secondnum = float(input('Second Number:'))
    except: 
        print('Invalid Input: Not a valid number')
        break
    
    if operator == 1:
        print('{0} + {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
        print(firstnum + secondnum)
        break
    if operator == 2:
        print('{0} - {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
        print(firstnum - secondnum)
        break
    if operator == 3:
        print('{0} * {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
        print(firstnum * secondnum)
        break
    if operator == 4:
        print('{0} / {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
        print(firstnum / secondnum)
        break
Formatting strings in Python
foo = 5
print('{foo}'.format(foo))

This is a nice way to format string, But there is a short and cleaner way to format strings in Python 3 known as the f-String. The problem with str.format() is that if you have multiple parameters. Your string starts to look a little werid.For example
first_name = &quot;Eric&quot;
last_name = &quot;Idle&quot;
age = 74
profession = &quot;comedian&quot;
affiliation = &quot;Monty Python&quot;
print((&quot;Hello, {first_name} {last_name}. You are {age}. &quot; + 
        &quot;You are a {profession}. You were a member of {affiliation}.&quot;) \
        .format(first_name=first_name, last_name=last_name, age=age, \
               profession=profession, affiliation=affiliation))

With f-strings. The same would look like.
print(f&quot;Hello, {first_name} {last_name}. You are {age}. &quot; +
      f&quot;You are a {profession}. You were a member of {affiliation}.&quot;)

This is why you should prefer using f-strings.
eval in Python
When you want to perform the arithmetic part, I see that you have used a thread of if-statements. But we can use Python's eval function to make life much easier. We simple place the operator entered by the user un between the two numbers.
old
if operator == 1:
    print('{0} + {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
    print(firstnum + secondnum)
    break
if operator == 2:
    print('{0} - {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
    print(firstnum - secondnum)
    break
if operator == 3:
    print('{0} * {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
    print(firstnum * secondnum)
    break
if operator == 4:
    print('{0} / {1} ='.format(firstnum, secondnum), end =&quot; &quot;)
    print(firstnum / secondnum)
    break

We ask the user to enter the operation the want directly , for example

Choose an operator: +

Or if you want to stick to using numbers.
operators = operators = {'+':1,'-':2,'*':3,'/':4}
I have gone with the 1st method. You can use whatever you prefer to
new
print(f&quot;{firstnum} {operator} {secondnum} = {float(eval(firstnum + operator + secondnum))}&quot;)

The new code using eval would like
operators = ['+','-','*','/']
while (True):
    # Gets operator, converts to int and breaks if its not a number
    try: operator = input(&quot;Choose an Operator:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\nInput\n(+,-,*,/):&quot;)
    except:
        print('Invalid Input: Choose A Valid Operation')
        break

    # Checks the operator is within 1 and 4
    if operator not in operators:
        print('Invalid Input: Choose A Valid Operation')
        break

    # Gets first and second number, converts numbers to float and breaks if its not a number
    try: firstnum = input('First Number:')
    except:
        print('Invalid Input: Not a valid number')
        break
    try: secondnum = input('Second Number:')
    except:
        print('Invalid Input: Not a valid number')
        break

    print(f&quot;{firstnum} {operator} {secondnum} = {float(eval(firstnum + operator + secondnum))}&quot;)

With that simple change, I removed about 20 lines of code into one. Making it much more readable
Clearing the screen
It would be nicer to see a clear terminal after finishing one job. There are many ways to clear the terminal in python. A common way for windows users is
os.system('cls')

But this way is considered bad as it is quite expensive. You can get away by simply
print(chr(27) + &quot;[2J&quot;)

Adding this to the start of each iteration in the while(True): loop will give a better experience to the user.
Waiting for input
Lastly, adding this line of code to the end will pause on the current text before going to the next one.
input(&quot;Press any key to continue...&quot;)