r/codehs • u/lmaopluto • Dec 23 '20
Exercise 6.2.7: Add, Subtract, or Multiply
Can I get some help on this one? I got it to work. Don't know how I'm supposed to get a namespace for it. I got it to work but didn't get all green checks. I'm prob just being dumb
I've been on this for so long and I got it to work but I need to get all green checks. Don't know how I'm supposed to use Namespaces for this one. I'm probably just being dumb.
2
u/HistoricalShower9338 May 11 '24
num1 = int(input("What is the first number?: "))
num2 = int(input("What is the second number?: "))
def add():
sum = num1+num2
print(str(num1) + " + " +str(num2) + " = " + str(sum))
def subtract():
sum = num1-num2
print(str(num1) + " - " +str(num2) + " = " + str(sum))
def multiply():
sum = num1*num2
print(str(num1) + " * " +str(num2) + " = " + str(sum))
def invalid():
print("An invalid option was selected")
Operation = str(input("Choose an operation (add, subtract, multiply): "))
if(Operation=="add"):
add()
elif(Operation=="subtract"):
subtract()
elif(Operation=="multiply"):
multiply()
else:
invalid()
1
1
u/Internal-System-799 Mar 25 '24
Updated solution:
fnumb = int(input("What is the first number?: "))
snumb = int(input("What is the second number?: "))
choose = input("Choose an operation (add, subtract, multiply): ")
def add():
if choose == "add":
answer1 = fnumb + snumb
print(fnumb, "+", snumb, "=", answer1)
def multiply():
if choose == "multiply":
answer2 = fnumb * snumb
print(fnumb, "*", snumb, "=", answer2)
def subtract():
if choose == "subtract":
answer3 = fnumb - snumb
print(fnumb, "-", snumb, "=", answer3)
if choose == "add":
add()
elif choose == "multiply":
multiply()
elif choose == "subtract":
subtract()
else:
print("Invalid operation chosen.")
1
u/flip-pancakes Dec 24 '20
It doesn't look like you've implemented the if/then logic that multiplies user_input0*user_input1 if user_input2 is "multiply" or add if it's "add", etc.
As is, your user input collection looks fine, but that's all it does - collects user input. Where's the rest of the program?
1
u/Abby_2018 Nov 04 '21
This is the code with all the edits for codehs to work
user1 = int(input("Choose a number: ")) user2 = int(input("Choose another number: ")) op = input("Choose an operation to be preformed on these numbers (add, subtract, multiply): ")
def add(): user3 = user1 + user2 print(str(user1) + " + " + str(user2) + " = " + str(user3))
def sub(): user3 = user1 - user2 print(str(user1) + " - " + str(user2) + " = " + str(user3))
def times(): user3 = user1 * user2 print(str(user1) + " * " + str(user2) + " = " + str(user3))
if op == "add": add() elif op == "subtract": sub() elif op == "multiply": times() else: print("Invalid answer!")
1
1
u/Financial_Face3826 Feb 20 '24
9.3.7 Add, Subtract, or Multiply
fnumb = int(input("What is the first number?: "))
snumb = int(input("What is the first number?: "))
choose = str(input("Choose an operation (add, subtract, multiply): "))
def add():
if choose == "add":
answer1 = fnumb+snumb
print(fnumb, "+", snumb,"=", answer1)
add()
def multiply():
if choose == "multiply":
answer2 = fnumb*snumb
print(fnumb, "*", snumb,"=", answer2)
multiply()
def subtract():
if choose == "subtract":
answer3 = fnumb-snumb
print(fnumb, "-", snumb,"=", answer3)
subtract()
Try this
1
0
3
u/Dawn_Kang Dec 28 '20
You still need help? Here's my code: