简体   繁体   中英

Why does Python tell me this is not defined?

So I am trying to write a code that uses functions to create a menu for user input. It's basically just inputting the amount of dollars you wish and picking which currency to convert it to. I am absolutely lost because every time I attempt to run this code I have so far (Just to make sure I'm on the correct path), I input "1" and say 20 dollars just for example, it tells me that "dollars" is not defined when I clearly have it as a user input.

def DisplayMenu():
    print("Choose a menu option:")
    print("1. European Euro")
    print("2. British Pound")
    print("3. Mexican Peso")
    print("4. Chinese Yuan")
    print("5. Japanese Yen")
    print("6. Quit")
    selection = int(input("Enter your selection: "))
    dollars = eval(input("Enter the dollar amount to convert: "))



def DollarConvert(selection, dollars):
    if selection == "1":
        conversion = dollars * 0.921
    elif selection == "2":
        conversion = dollars * 0.807
    elif selection == "3":
        conversion = dollars * 24.246
    elif selection == "4":
        conversion = dollars * 7.085
    elif selection == "5":
        conversion = dollars * 108.03
    elif selection == "6":
        quit
    elif selection > 6:
        print("Invalid input.")


DisplayMenu()

print("$ ",dollars," = ",chr(8364),conversion)

Hopefully someone can help me with this because I am out of ideas

You never run or from DollarConvert(), or receive outputs from either function. Change the functions to return values, like this:

def DisplayMenu():
    print("Choose a menu option:")
    print("1. European Euro")
    print("2. British Pound")
    print("3. Mexican Peso")
    print("4. Chinese Yuan")
    print("5. Japanese Yen")
    print("6. Quit")
    selection = int(input("Enter your selection: "))
    dollars = eval(input("Enter the dollar amount to convert: "))
    return selection, dollars

def DollarConvert(selection, dollars):
    if selection == "1":
        return = dollars * 0.921
    elif selection == "2":
        return = dollars * 0.807
    elif selection == "3":
        return dollars * 24.246
    elif selection == "4":
        return dollars * 7.085
    elif selection == "5":
        return dollars * 108.03
    elif selection == "6":
        quit
    elif selection > 6:
        print("Invalid input.")
        return None


selection, dollars = DisplayMenu()
conversion = DollarConvert(selection, dollars)
print("$ ",dollars," = ",chr(8364),conversion)

There we go.

In your code, the dollars variable's scope is limited to inside that function. You have to return dollars from the function to make it accessible. Also your dollar convert function is never called, so you need to call it. You also need to add a return statement for the conversion variable. Returning is essential for most all functions, otherwise data is lost.

You could also do it using the global keyword

dollars = None
conversion = None
def DisplayMenu():
    global dollars
    print("Choose a menu option:")
    print("1. European Euro")
    print("2. British Pound")
    print("3. Mexican Peso")
    print("4. Chinese Yuan")
    print("5. Japanese Yen")
    print("6. Quit")
    selection = input("Enter your selection: ")
    dollars = int(input("Enter the dollar amount to convert: "))
    DollarConvert(selection, dollars)


def DollarConvert(selection, dollars):
    global conversion
    if selection == "1":
        conversion = dollars * 0.921
    elif selection == "2":
        conversion = dollars * 0.807
    elif selection == "3":
        conversion = dollars * 24.246
    elif selection == "4":
        conversion = dollars * 7.085
    elif selection == "5":
        conversion = dollars * 108.03
    elif selection == "6":
        quit
    elif selection > 6:
        print("Invalid input.")


DisplayMenu()

print("$ ",dollars," = ",chr(8364),conversion)

Also, one more mistake on line 12 & 13, ie

selection = int(input("Enter your selection: "))

changed to

selection = input("Enter your selection: ")

and

dollars = eval(input("Enter the dollar amount to convert: "))

changed to

dollars = int(input("Enter the dollar amount to convert: "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM