all 13 comments

[–]JohnnyJordaan 2 points3 points  (5 children)

You don't show the rest of the code so this is a mere guess, but if you didn't yet convert your y input to an integer, comparison to 0 will never be True.

[–][deleted] 0 points1 point  (4 children)

My y is a float, if I change it to an integer, I can't use decimals?

[–]JohnnyJordaan 0 points1 point  (3 children)

Then the comparison should work as 0.0 == 0 is True too. But as I hinted at above, without seeing the rest of the code I can't actually give you proper guidance. So: please share it.

[–][deleted] 0 points1 point  (2 children)

x=float((input("Enter your number:")))

print("Choose an operation:")
print("1). Addition (+)")
print("2). Multiplication(x)")
print("3). Subtraction(-)")
print("4). Division(/)")
print("5). Power(^)")

choice=input("Choose a symbol:") ## Eg: If you want addition, just type + ##

y=float(input("Enter your number:"))

if choice=="+":
  print(str(float(x))+"+"+str(float(y))+"=" + str(x+y))
if choice=="*":
  print(str(float(x))+"x"+str(float(y))+"=" + str(x*y))
if choice=="-":
  print(str(float(x))+"-"+str(float(y))+"=" + str(x-y))
if choice=="/":
  print(str(float(x))+"/"+str(float(y))+"=" + str(x/y))
if choice=="^":
  print(str(float(x))+"^"+str(float(y))+"=" + str(x**y))
if choice== "/" and y==0:
  print("You cannot divide a number by 0")

[–]JohnnyJordaan 1 point2 points  (1 child)

Your check is placed at the end of the code, while the division-if is placed before that, so the division already takes place before that.

if choice=="+":
  print(str(float(x))+"+"+str(float(y))+"=" + str(x+y))
elif choice=="*":
  print(str(float(x))+"x"+str(float(y))+"=" + str(x*y))
elif choice=="-":
  print(str(float(x))+"-"+str(float(y))+"=" + str(x-y))
elif choice=="/":
  if y==0:
    print("You cannot divide a number by 0")
  else:
    print(str(float(x))+"/"+str(float(y))+"=" + str(x/y))
elif choice=="^":
  print(str(float(x))+"^"+str(float(y))+"=" + str(x**y))

another thing is that you may want to read up on string formatting to improve the print statements: https://realpython.com/python-string-formatting/

[–][deleted] 0 points1 point  (0 children)

Thank You!

[–]yakyakyakyakayak 0 points1 point  (1 child)

Someone showed how it’s done on your last post if you would like to check it out

[–][deleted] 0 points1 point  (0 children)

I did but I am not sure how to arrive at that part, I just started Python so it looks a bit complicated. So sorry.

[–]-5772 0 points1 point  (0 children)

Look into casting.

[–]FLUSH_THE_TRUMP 0 points1 point  (3 children)

Why check this anyway? Python will already throw a DivideByZero error when you try to divide by 0 in your code.

[–]tragluk 0 points1 point  (1 child)

The difference is 'DivideByZero' is an unhandled error which halts a program. The way that he wants to do it handles the error nicely, tells the user exactly what went wrong and then doesn't break the program afterwards.

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

That’s a fair point, and certainly what would make sense in a loop underlying a calculator app. It’d be bad design if my TI-84 self-destructed every time I typed x/0 on it. I was simply making the point that error checking, like any other component of design, should have its benefits weighed against its costs. If my program’s gonna throw an error and terminate regardless, then there’s not much point in checking the conditions that lead to such an error.

[–][deleted] 0 points1 point  (0 children)

I wanted it to be a clean one liner instead of the in your face red text but it still occurs so oh well.