\$\begingroup\$
\$\endgroup\$
1
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
riskypenguin
3,4931 gold badge10 silver badges28 bronze badges
-
1\$\begingroup\$ Welcome and thanks for joining CodeReview! I hope that you get some good feedback; this is a great first question. \$\endgroup\$Reinderien– Reinderien2021年06月25日 15:19:16 +00:00Commented Jun 25, 2021 at 15:19
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
- 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
whileloop doesn't deserve to exist; the condition is only evaluatedTrueonce - 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
answered Jun 25, 2021 at 15:32
Reinderien
71.1k5 gold badges76 silver badges256 bronze badges
-
\$\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– crazyrocky2021年06月25日 17:20:01 +00:00Commented Jun 25, 2021 at 17:20
-
1\$\begingroup\$ Lines like
first_number = "a"that get overwritten before they're read. \$\endgroup\$Reinderien– Reinderien2021年06月25日 17:21:02 +00:00Commented Jun 25, 2021 at 17:21
You must log in to answer this question.
lang-py