[Tutor] How do I fix this IndexError?
Nathan Pinno
falcon3166 at hotmail.com
Tue Dec 20 22:48:40 CET 2005
Here is the error:
The Currency Exchange Program
By Nathan Pinno
Traceback (most recent call last):
File "D:\Python24\exchange.py", line 28, in -toplevel-
exch = pickle.load(store)
File "D:\Python24\lib\pickle.py", line 1390, in load
return Unpickler(file).load()
File "D:\Python24\lib\pickle.py", line 872, in load
dispatch[key](self)
File "D:\Python24\lib\pickle.py", line 1207, in load_appends
mark = self.marker()
File "D:\Python24\lib\pickle.py", line 888, in marker
while stack[k] is not mark: k = k-1
IndexError: list index out of range
and here is the relevant code:
[code]
import pickle
rates = {'can_us' : 0.80276,
'us_can' : 1.245702,
'can_euro' : 1.488707,
'euro_can' : 0.671724}
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"
store = open('exch.txt', 'rb')#load
exch = pickle.load(store)
store.close()
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:
store = open("exch.txt", 'wb') #save
pickle.dump(exch, store)
store.close()
break
print "Goodbye."
[/code]
How do I fix the IndexError?
Thanks,
Nathan Pinno,
<http://www.the-web-surfers-store.com/>
MSN Messenger: falcon3166 at hotmail.com
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051220/5ce4be69/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 862 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20051220/5ce4be69/attachment-0001.gif
More information about the Tutor
mailing list