简体   繁体   中英

how do i add a phrase to a float (decimal) number in python?

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.

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

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)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM