Ask coding questions

← Back to all posts
calculator

import time
addvariable = input("Give me a number")
addnumber = input("give me a second number")
print("your answer is...")
time.sleep(1)
print(addnumber + addvariable)

timesvariable = input("give me a number")
timesnumber = input("give me a second number")
print("your answer is...")
time.sleep(1)
print(timesvariable * timesnumber)
why cant i get my code to multiply or add???!!!!

Answered by ReshiramWolfu (61) [earned 5 cycles]
View Answer
Commentshotnewtop
ReshiramWolfu (61)

is this Python? If so, then you need to declare the variables.
timesvariable = int()
addvariable = int()
timesnumber = int()
addnumber = int()
add them before the input, and then to fix the input, do
timesnumber = int(input("Enter a Number"))
Make sure to close both brackets

James193 (8)

@ReshiramWolfu thank you soc much :)!!!!

James193 (8)

@ReshiramWolfu ive now tried to add some if statments for the user to choose if they want addition or multiplication but they dont work!!!

timesvariable = int()
addvariable = int()
timesnumber = int()
addnumber = int()
import time
mat = input("what would you like to do???, 1 = addition, 2 = multiplication")
if mat == 1:
addvariable = int(input("Give me a number"))
addnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(addnumber + addvariable)
else mat == 2:
timesvariable = int(input("give me a number"))
timesnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(timesvariable * timesnumber)

ReshiramWolfu (61)

@James193 here is the fixed version...
timesvariable = int()
addvariable = int()
timesnumber = int()
addnumber = int()
mat = bool()
import time
mat = input("what would you like to do???, True = addition, False = multiplication")
if mat == True:
addvariable = int(input("Give me a number"))
addnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(addnumber + addvariable)
elif mat == False:
timesvariable = int(input("give me a number"))
timesnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(timesvariable * timesnumber)

James193 (8)

@ReshiramWolfu ive tried it but for some reason it dosent allow you to input the numbers???!!!

ReshiramWolfu (61)

@James193 Have you indented the things in the if and elif blocks?

ReshiramWolfu (61)

@James193 i will try to figure it out

James193 (8)

@ReshiramWolfu yes i have indented the things in the if and elif blocks,

ReshiramWolfu (61)

@James193 I will sort it later. At School right now

James193 (8)

@ReshiramWolfu
ive done it!!!! all we needed to do was but " " around the True and False
here is the code
timesvariable = int()
addvariable = int()
timesnumber = int()
addnumber = int()
mat = bool()
import time
mat = input("what would you like to do???, True = addition, False = multiplication")
if mat == "True":
addvariable = int(input("Give me a number"))
addnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(addnumber + addvariable)
elif mat == "False":
timesvariable = int(input("give me a number"))
timesnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(timesvariable * timesnumber)

James193 (8)

@ReshiramWolfu thank you so much for all your efforts!!!

James193 (8)

@ReshiramWolfu here is the full version of my code now thanks to you!!!:)

while True:
timesvariable = int()
addvariable = int()
timesnumber = int()
addnumber = int()
divisionnumber = int()
divisionvariable = int()
subnumber = int()
subvariable = int()
mat = bool()
import time
mat = input("what would you like to do???, 1 = addition, 2 = multiplication, 3 = division or 4 = subtraction or break if you want to exit the calculator")
if mat == "1":
addvariable = int(input("Give me a number"))
addnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(addnumber + addvariable)
elif mat == "2":
timesvariable = int(input("give me a number"))
timesnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(timesnumber * timesvariable)
elif mat == "3":
divisionvariable = int(input("give me a number"))
divisionnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(divisionvariable / divisionnumber)
elif mat == "4":
subvariable = int(input("give me a number"))
subnumber = int(input("give me a second number"))
print("your answer is...")
time.sleep(1)
print(subvariable - subnumber)
elif mat == "break":
break

ReshiramWolfu (61)

@James193 Well done! Your calculator is awesome! The next step up is to add powers of numbers and power roots of numbers. And to add floats instead of ints (Decimal instead of integer)

pyelias (659)

@James193 Just so you're aware, you don't need the `number = int()` lines in Python.

ReshiramWolfu (61)

@pyelias I learned that way, so i always declare the variables first. It is just the way i learned

NKY_28181503 (0)

input returns a string... i think

katyadee (1087)

Hey guys! Great work here. Remember that you can add your repls, and to always add context to your questions so other community members can learn from them too :-)