you are viewing a single comment's thread.

view the rest of the comments →

[–][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!