0

Writing a simple calculator. Need to do addition, subtraction, multiplication, float division, integer division, modulus and exponent. For division the code is repeating and asking for 4 numbers although I have specified it to only ask 2.


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Float Division")



choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
fnum1 = float(input("Enter first number: "))
fnum2 = float(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
    print(fnum1, "/",fnum2,"=", divide(fnum1,fnum2))
else:    
 print("Invalid")


When I run the module and select division I want to enter my 2 numbers and have an answer. For some reason it asks for num1, num2, fnum1 and fnum2.

  • 4
    Because that is exactly what your code is saying it will do? Why would you expect it to *not* do that - there is nothing in it that would prevent that – UnholySheep Apr 15 '19 at 19:54
  • If you call `input` four times, your program will ask for input four times. Use `num1` to define `fnum1` instead of calling `input` again. – Patrick Haugh Apr 15 '19 at 19:56

1 Answers1

1

Order of operation my dude. Python will read top down and do exactly what you tell it to do. You told it to input four times, so it will input four times. If you don't want this then you need to change your code.

print("4.Float Division")
choice = input("Enter choice(1/2/3/4):")
if choice == '1' or choice == '2':
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))
else:
    fnum1 = float(input("Enter first number: "))
    fnum2 = float(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
    print(fnum1, "/",fnum2,"=", divide(fnum1,fnum2))
else:    
 print("Invalid")

That being said, in programming you should follow the DRY principle. (i.e. Don't Repeat Yourself)

print("4.Float Division")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1' or choice == '2':
    num1, num2 = int(num1), int(num2)
else:
    num1, num2 = float(num1), float(num2)

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
    print(num1, "/",num2,"=", divide(num1,num2))
else:    
 print("Invalid")

There is also a way to significantly shorten the code above but I believe such a drastic step would confuse you and possibly deter you.

  • Wow thanks a ton for the reply. I'm a total beginner at this and have zero programming experience prior to this. I'm now trying to run it and it won't print the output for any of my statements. I don't understand what I'm missing – user11365096 Apr 15 '19 at 20:16
  • Did you implement the methods `add`, `subtract`, etc? Or did you mean to do `num1 + num2` etc.... – Error - Syntactical Remorse Apr 15 '19 at 20:18
  • If python had case statements I'd suggest using case statements, but since you'd need a dict to implement it, I agree with your decision not to confuse OP even more. For those that DO want to investigate further, look at this SO answer https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python – Nick Vitha Apr 15 '19 at 20:20
  • I would have used a `str` to operator solution that would not have required a `if` or `switch` such as [Turn string into operator](https://stackoverflow.com/a/38095600/8150685). – Error - Syntactical Remorse Apr 15 '19 at 20:28
  • I am so confused right now this is insane. It sucks I'm gonna fail this class, I don't understand any of this. Thanks for trying to help, I'm just a lost cause. – user11365096 Apr 15 '19 at 20:28
  • @user11365096 Need some help? I can explain stuff you don't understand: [writing-a-simple-calculator](https://chat.stackoverflow.com/rooms/191874/writing-a-simple-calculator) – Error - Syntactical Remorse Apr 15 '19 at 20:31
  • That would be great, just looks like I need 20 reputation to reply – user11365096 Apr 15 '19 at 20:43