[Tutor] How do I fix this IndexError?

Nathan Pinno falcon3166 at hotmail.com
Wed Dec 21 00:10:04 CET 2005


Thanks Simon!

It worked like a charm! All I have to do now is change it into GUI format,
and then use py2exe to create an executable file. 

Thanks for all the help!
Nathan Pinno
MSN Messenger: falcon3166 at hotmail.com
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705  

-----Original Message-----
From: Simon Gerber [mailto:nequeo at gmail.com] 
Sent: December 20, 2005 3:55 PM
To: Nathan Pinno
Cc: tutor at python.org
Subject: Re: [Tutor] How do I fix this IndexError?

Hi Nathan,

I've attached a crude hack that fixes the problem. It should give you an
idea of how to proceed. I've put comments next to the things I've changed.
Bear in mind I'm hardly more than a beginner myself, so there are definitely
better ways to do this. But it's somewhere to start, anyway.

Cheers!

[code]

import pickle, os     #LOAD THE OS MODULE

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"

if os.path.exists('exch.txt'): # Check to see if 'exch.txt' exists
    store = open('exch.txt', 'rb')  # Only open the file if it exists
    rates = pickle.load(store)    # Save as 'rates', not 'exch'
    store.close()
else:
    # If there's no exch.txt file, use these default rates.
    rates = {'can_us' : 0.80276,
                'us_can' : 1.245702,
                'can_euro' : 1.488707,
                'euro_can' : 0.671724}

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(rates, store)     # Save 'rates' variable, not 'exch'.
        store.close()
        break
print "Goodbye."


--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
    screensavers'


More information about the Tutor mailing list