[Tutor] How do I fix this error so that my exchange rates program will work?
Nathan Pinno
falcon3166 at hotmail.com
Wed Dec 14 22:58:46 CET 2005
I added the dictionary and loading and saving for the rates in my exchange currency rates program, and when I ran it, I got the following error:
Traceback (most recent call last):
File "D:\Python24\exchange.py", line 84, in -toplevel-
save_rates(rates)
File "D:\Python24\exchange.py", line 9, in save_rates
store.write(rate + '\n')
TypeError: unsupported operand type(s) for +: 'float' and 'str'
Here is the program's code:
rates = {'can_us' : 0.80276,
'us_can' : 1.245702,
'can_euro' : 1.488707,
'euro_can' : 0.671724}
def save_rates(exch):
store = open("exch.txt","w")
for exch, rate in rates.items():
store.write(exch + '\n')
store.write(rate + '\n')
store.close()
def load_rates(exch):
import os
filename = 'exch.txt'
if os.path.exists(filename):
store = open(filename,'r')
for line in store:
exch = line.strip()
rates = store.next().strip()
exch[rates] = rate
else:
store = open(filename,'w') # create new empty file
store.close()
def menu():
print "1. Change Canadian currency into American."
print "2. Change American currency into Canadian."
print "3. Change Canadian currency into Euros."
print "4. Change Euros into Canadian currency."
print "5. Update exchange rates."
print "9. Save and Exit"
def exchange_update():
print "1. Update Canadian to US rate."
print "2. Update US to Canadian rate."
print "3. Update Canadian to Euro rate."
print "4. Update Euro to Canadian update."
print "5. Main menu"
def menu_choice():
return int(raw_input("Which option? "))
print "The Currency Exchange Program"
print "By Nathan Pinno"
load_rates(rates)
while 1:
menu()
menu_option = menu_choice()
if menu_option == 1:
can = float(raw_input("Canadian $"))
print "US $",can*rates['can_us']
elif menu_option == 2:
us = float(raw_input("US $"))
print "CAN $",us*rates['us_can']
elif menu_option == 3:
can = float(raw_input("CAN $"))
print "Euros",can*rates['can_euro']
elif menu_option == 4:
euro = float(raw_input("Euros"))
print "CAN $",euro*rates['euro_can']
elif menu_option == 5:
while 1:
exchange_update()
sub = menu_choice()
if sub == 1:
new_can = float(raw_input("New CAN-US Exchange rate: "))
rates['can_us'] = new_can
print "Exchange rate successfully updated!"
elif sub == 2:
new_us = float(raw_input("New US-CAN Exchange rate: "))
rates['us_can'] = new_us
print "Exchange rate successfully updated!"
elif sub == 3:
new_cxr = float(raw_input("New CAN-Euro Exchange rate: "))
rates['can_euro'] = new_cxr
print "Exchange rate successfully updated!"
elif sub == 4:
new_euro = float(raw_input("New Euro-CAN Exchange rate: "))
rates['euro_can'] = new_euro
print "Exchange rate successfully updated!"
elif sub == 5:
break
elif menu_option == 9:
save_rates(rates)
break
print "Goodbye."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051214/1f1b891d/attachment.html
More information about the Tutor
mailing list