Reputation: 25
I am trying to make a currency convertor using two functions in one program but for some reason my program either gets this error:
line 23, in Input1
print(amount+"Won equals to "+totalamount+" USD")
TypeError: unsupported operand type(s) for +: 'float' and 'str'
Or does not print anything after putting the 'Option' and 'Amount'. This is my program:
print("Please choose which currency you want to convert:")
print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")
print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")
print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")
print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")
print("E - Quit")
usd = 0.000905
eur = 0.000807350908
yen = 0.0919061643
rmb = 0.00603703605
def main():
option =input("Enter your option: ")
if option== "E":
exit()
amount =float(input("Enter the amoutn in Korean Won: "))
Input1(option, amount)
def Input1(option,amount):
if option == "A":
totalamount = (amount * usd)
print(amount+"Won equals to "+totalamount+" USD")
elif option== "B":
totalamount = (amount * eur)
print (amount+"Won equals to "+totalamount+" Euro")
elif option== "C":
totalamount = (amount * yen)
print (amount+"Won equals to "+totalamount+" Yen")
elif option== "D":
totalamount = (amount * rmb)
print (amount+"Won equals to "+totalamount+" Chinese RMB")
else:
print("You entered an invalid input.")
return option,amount
main()
Can you guys give me tips to fix my program? I am still learning python and any help would be greatly appreciated. Thank you so much!
Upvotes: 0
Views: 378
Reputation: 701
You just need to typecast amount and totalamount variable that is currently float into string
print(str(amount)+"Won equals to "+str(totalamount)+" USD")
Or use you use format function
print("{} Won equals to {} USD ".format(amount, totalamount))
And i think format function is better option
Upvotes: 3
Reputation: 71522
Use an f-string instead of string concatenation; it'll handle the conversion for you!
print(f"{amount} won equals to {totalamount} USD")
If you did want to do the conversion yourself, it'd look like this:
print(str(amount) + "Won equals to " + str(totalamount) + " USD")
(edit) to answer the question asked in comments, change your main()
function to:
def main():
while True:
option = input("Enter your option: ")
if option== "E":
break
amount = float(input("Enter the amount in Korean Won: "))
Input1(option, amount)
Upvotes: 0
Reputation: 49
You have to convert amount
and totalamount
to a string before using it in print()
as string with +. You could use the str()
method.
print(str(amount)+"Won equals to "+str(totalamount)+" USD")
Upvotes: 0