python - error printing Integer with strings -
i beginner python trying write not working. help?
cp=input("enter cost price: ") sp=input("enter sale price: ") if (sp>cp): print ("congratulations !! made profit of ", + sp-cp) print("congratulations!! made profit of %f" % sp-cp) elif (cp>sp): print("you in loss of %f" % cp-sp) else: print("you got nothing")
in python 3 (which i'm assuming using because of python-3.x tag included), input function returns string, , can't math on string. need change
cp=input("enter cost price: ") sp=input("enter sale price: ") to
cp = int( input("enter cost price: ") ) sp = int( input("enter sale price: ") ) (spaces added show changed).
as answer above said, need add parenthesis around substitution values
print("congratulations!! made profit of %f" % sp-cp) becomes
print("congratulations!! made profit of %f" % (sp-cp) ) (edit: benjamin beat me 3 seconds!)
Comments
Post a Comment