HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Beginner's simple calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
beginnersimplecalculator

Problem

What do you think about this simple calculator?

```
while True:
print('Options:')
print('Enter \'a\' to add two numbers')
print('Enter \'s\' to subtract two numbers')
print('Enter \'m\' to multiply two numbers')
print('Enter \'d\' to divide two numbers')
print('Enter \'q\' to quit the program')
response=str(input(':')).lower()
if response=='q':
break
elif response=='a':
while True:
try:
num1=float(input('Enter a number:'))
break
except(TypeError,ValueError):
print('(Input Error) Enter valid number')
continue
while True:
try:
num2=float(input('Enter another number:'))
break
except(TypeError,ValueError):
print('(Input Error) Enter valid number')
continue
result=num1+num2
print('The answer is '+str(result))
print()
elif response=='s':
while True:
try:
num1=float(input('Enter a number:'))
break
except(TypeError,ValueError):
print('(Input Error) Enter valid number')
continue
while True:
try:
num2=float(input('Enter another number:'))
break
except(TypeError,ValueError):
print('(Input Error) Enter valid number')
continue
result=num1-num2
print('The answer is '+str(result))
print()
elif response=='m':
while True:
try:
num1=float(input('Enter a number:'))
break
except(TypeError,ValueError):
print('(Input Error) Enter valid number')
continue
while True:
try:
num2=float(input('Enter another number:'))
break
except(TypeError,ValueError):

Solution

Welcome to the world of beginner Python programming, and programming in general! Python is a nice language to learn and start out with!

Allow me to give you some tips and pointers, with the hope that you will be able to learn a little from them.

Also, allow me to apologize in advance for this massive wall of text. I may have gone a little bit overboard in my review and analysis, but I wanted to make sure you would understand my revisions and some of the code I've added, in the event you are unfamiliar with the functionality I implement and use in the reviewed and revised code.

Add white space for readability, and obey PEP8 rules for whitespace around assignment and equality operators

Python is extremely indentation-based. This means an indent usually indicates a code block that needs executed underneath of another one (such as your if statements have, etc).

However, Python also benefits from having white space added at logical locations for readability. I usually define these locations as between loops and other sections of code, between functional statements (addition, subtraction, etc.) and output, between the end of an if statement and the beginning of its elif or else blocks, after the end of an entire if block, etc.

There is also a rule for Python code style, called the PEP8 which dictates whitespace around assignment statements, equality operators (such as ==), and also arithmetic operators (+, -, *, /, and others), stating that a space is necessary around them.

This is your code with the whitespace per my guidelines and the PEP8 guidelines. My further reviews from here will be using this as a base.

while True:
    print('Options:')
    print('Enter \'a\' to add two numbers')
    print('Enter \'s\' to subtract two numbers')
    print('Enter \'m\' to multiply two numbers')
    print('Enter \'d\' to divide two numbers')
    print('Enter \'q\' to quit the program')

    response = str(input(':')).lower()

    if response == 'q':
        break

    elif response == 'a':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 + num2

        print('The answer is ' + str(result))
        print()

    elif response == 's':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 - num2

        print('The answer is ' + str(result))
        print()

    elif response == 'm':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 * num2

        print('The answer is ' + str(result))
        print()

    elif response == 'd':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 / num2

        print('The answer is ' + str(result))
        print()

    else:
        print('*Unknown input*')

    continue


Move non-quit responses to separate "if" block, so we can do some tasks without repeating code.

One of the major guidelines of programming is that if you are doing something that requires you to copy and paste code in all the time to do the same thing, chances are there's a better implementation.

You currently do this in every single block of code under each conditional check for the response entered, except for quitting and invalid responses:

```
while True:
try:

Code Snippets

while True:
    print('Options:')
    print('Enter \'a\' to add two numbers')
    print('Enter \'s\' to subtract two numbers')
    print('Enter \'m\' to multiply two numbers')
    print('Enter \'d\' to divide two numbers')
    print('Enter \'q\' to quit the program')

    response = str(input(':')).lower()

    if response == 'q':
        break

    elif response == 'a':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 + num2

        print('The answer is ' + str(result))
        print()

    elif response == 's':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 - num2

        print('The answer is ' + str(result))
        print()

    elif response == 'm':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 * num2

        print('The answer is ' + str(result))
        print()

    elif response == 'd':
        while True:
            try:
                num1 = float(input('Enter a number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        while True:
            try:
                num2 = float(input('Enter another number:'))
                break
            except(TypeError, ValueError):
                print('*(Input Error) Enter valid number*')
                continue

        result = num1 / num2

        print('The answer is ' + str(result))
        print()

    else:
        print('*Unknown input*')

    continue
while True:
    try:
        num1 = float(input('Enter a number:'))
        break
    except(TypeError, ValueError):
        print('*(Input Error) Enter valid number*')
        continue

while True:
    try:
        num2 = float(input('Enter another number:'))
        break
    except(TypeError, ValueError):
        print('*(Input Error) Enter valid number*')
        continue
if response == 'q':
    break

while True:
    try:
        num1 = float(input('Enter a number:'))
        break
    except(TypeError, ValueError):
        print('*(Input Error) Enter valid number*')
        continue

while True:
    try:
        num2 = float(input('Enter another number:'))
        break
    except(TypeError, ValueError):
        print('*(Input Error) Enter valid number*')
        continue

if response == 'a':
    result = num1 + num2

elif response == 's':
    result = num1 - num2

elif response == 'm':
    result = num1 * num2

elif response == 'd':
    result = num1 / num2
if response == 'q':
    break
elif response not in 'asmd':
    print('*Unknown input*')
    continue
if response == 'q':
    break
elif len(response) > 1 or response not in 'asmd':
    print('*Unknown input*')
    continue

Context

StackExchange Code Review Q#149797, answer score: 12

Revisions (0)

No revisions yet.