1

So I'm working on a code to divide numbers and its dealing with if statements and stuff. What I have currently is:

num= int(input( "Enter the numerator: "))  

den= int(input( "Enter the denominator: "))   

if (den==0):  
    print ("Error - cannot divide by zero")  
else:  
    print ("Decimal: ") + (num/den)  

Well, my issue is on that last line of code. I have no idea what I'm doing wrong, I've tried float, str, and int and every time it gives me an issue.

AChampion
  • 29,683
  • 4
  • 59
  • 75
  • 1
    `print("Decimal:", num/den)` alternatively look at `str.format()` – AChampion Apr 05 '17 at 03:27
  • 3
    Possible duplicate of [How to format a floating number to fixed width in Python](http://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python) – McGrady Apr 05 '17 at 03:27

2 Answers2

0

There is little syntax error in the last line of the code. Also, covert float to string before concatenation else it will traceback with a TypeError.

num= int(input( "Enter the numerator: "))  

den= int(input( "Enter the denominator: "))   

if (den==0):  
    print ("Error - cannot divide by zero")  
else:  
    print ("Decimal: " + str(num/den))  # Check syntax. Convert float to string
Mr.Pacman
  • 350
  • 2
  • 7
0

try this

from future import division

num ,den = map(int,raw_input().split()) #enter space separate input

if den==0:

print "Error - cannot divide by zero" 

else:

print "Decimal: " + str(num/den)