4
\$\begingroup\$

Second day coding so I'm new to this. I have made this simple calculator but I repeat the code in order to use the previous answer in the next calculation. I've been trying to simplify it but I can't seem to figure it out. Whatever I try breaks the code. Any feed back helps. Here is the code

# Beginning calculator code
first_number = "a"
second_number = ""
carry_number = ""
operation = ""
while first_number == "a":
    first_number = float(input("Enter your first number: "))
    operation = input("You can enter c to exit calculator \nWhat operation? ")
    second_number = float(input("What is your second number? "))
    if operation == "+":
        carry_number = (first_number + second_number)
    elif operation == "-":
        carry_number = (first_number - second_number)
    elif operation == "/":
        carry_number = (first_number / second_number)
    elif operation == "*":
        carry_number = (first_number * second_number)



    print(first_number, operation, second_number, "=", carry_number)
    first_number = carry_number
while True:
    operation = input("You can enter c to exit calculator \nWhat operation? ")
    if operation == "c":
        quit()
    second_number = float(input("What is your second number? "))
    if operation == "+":
        carry_number = (first_number + second_number)
    elif operation == "-":
        carry_number = (first_number - second_number)
    elif operation == "/":
        carry_number = (first_number / second_number)
    elif operation == "*":
        carry_number = (first_number * second_number)

    print(first_number, operation, second_number, "=", carry_number)
    first_number = carry_number
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome and thanks for joining CodeReview! I hope that you get some good feedback; this is a great first question. \$\endgroup\$
    – Reinderien
    Jun 25 '21 at 15:19
4
\$\begingroup\$
  • Notice that you have a bunch of repeated code; let's reduce this
  • There's no need to pre-initialize your variables in the first five lines of your code
  • Your first while loop doesn't deserve to exist; the condition is only evaluated True once
  • No need to surround your operations in parens

There's a lot of other things you could do to improve this, but without getting too advanced, you could write this to be as simple as

# Beginning calculator code

first_number = float(input('Enter your first number: '))

while True:
    print('You can enter c to exit calculator')
    operation = input('What operation? ')
    if operation == 'c':
        quit()
        
    second_number = float(input('What is your second number? '))
    if operation == '+':
        carry_number = first_number + second_number
    elif operation == '-':
        carry_number = first_number - second_number
    elif operation == '/':
        carry_number = first_number / second_number
    elif operation == '*':
        carry_number = first_number * second_number

    print(first_number, operation, second_number, '=', carry_number)
    first_number = carry_number
\$\endgroup\$
2
  • \$\begingroup\$ First off thank you for the explanation. It made sense to me right away when you wrote it like that. So when you said I don't need to pre-initialize the variables you meant the lines that I wrote "first number = second number = etc..". The program automatically can keep track of the values? \$\endgroup\$
    – crazyrocky
    Jun 25 '21 at 17:20
  • 1
    \$\begingroup\$ Lines like first_number = "a" that get overwritten before they're read. \$\endgroup\$
    – Reinderien
    Jun 25 '21 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.