2017-02-06 5 views
1

Ich brauche Hilfe, das sind die Fehler: Was mache ich falsch?Übertragen von Variablen von einer Funktion zur nächsten in Python

Traceback (most recent call last): 
    File "python", line 64, in <module> 
    File "python", line 6, in walmart 
    File "python", line 28, in shopping 
    File "python", line 53, in drink 
    File "python", line 61, in total_price 
NameError: global name 'price' is not defined 

Mein Code:

def walmart(): 
    print "Hello welcome to the store!" 
    answer = raw_input("What's your name?") 
    if len(answer) > 0: 
     print "okay great %s, Lets go!...." % (answer) 
     shopping() 
    else: 
     print "Sorry try typing something" 
     walmart() 
def shopping(): 
    print "Ok lets get shopping" 
    shop_list = {"pizza" : 10.00 , "fries" : 15.00 , "burger" : 15.00} 
    print "Here are a list of food avaliable..." 
    print shop_list 
    ans1 = raw_input("Please select your item...").lower() 
    price = shop_list[ans1] 

    if "pizza" in ans1: 
     print "Your current price is... " + str(shop_list[ans1]) 
     drink(price) 

    elif "burger" in ans1: 
     print "Your current price is... " + str(shop_list[ans1]) 
     drink(price) 

    elif "fries" in ans1: 
     print "Your current price is... " + str(shop_list[ans1]) 
     drink(price) 

    else: 
     print "Please type something on the list..." 
     shopping() 
    return price 

def drink(price): 
    print "Okay let's pick you a drink" 
    drink_list = {"water" : 1 , "soda" : 2 , "tea" : 3} 
    print "Here is a list of drinks..." 
    print drink_list 
    ans2 = raw_input("Please type your choice here...").lower() 
    price_drink = drink_list[ans2] 

    if "water" in ans2: 
     print "Great healthy choice!" 
     total_price(price_drink) 

    elif "soda" in ans2: 
     print "Not that heaalthy but whatever floats your boat!" 
     total_price(price_drink) 

    elif "tea" in ans2: 
     print "OOOOO Tea great choice " 
     total_price(price_drink) 

    else: 
     print " Try again!" 
     drink(price) 
    return price_drink 

def total_price(price_drink): 
    totalprice = drink(price) + shopping() 
    print "Thanks for shopping....\nHere is your total price..." 
    print totalprice 
walmart() 
+0

So wie der Fehler sagt, "Preis" ist nicht definiert in 'total_price'. Was ist deine Frage? – Carcigenicate

Antwort

2

Das Problem ist Ihre Variable "Preis" lokale Variable ist und existieren nur innerhalb der Funktion, also in Funktion TOTAL_PRICE, variable "Preis" existiert nicht. Sie könnten das beheben, indem Sie die Variable "price" zu einer globalen Variablen machen, indem Sie sie außerhalb von Funktionen definieren.

# Before functions 

price = 0 

# your functions go here 
def ...... 
+0

danke, krank versuche das! –

2

Sie nicht Transfer Variablen von einer Funktion zur anderen. Wenn Sie eine Variable in mehrere Funktion verwenden mögen, was Sie tun können, ist diese Variable global definieren und anschließend in verschiedenen Funktionen verwenden

global_var = 10 

def func1(): 
    global global_var 
    #rest of the function 

def func1(): 
    global global_var 
    #rest of the function 

UPDATE ich über den Kommentar unten dachte, und ich dachte, dass ich dies teilen sollte mit dir. Obwohl in Ihrem Fall globale Variable scheint eine gute Wahl, aber bedenken Sie, dass die Verwendung von Globals nicht als gute Praxis betrachtet werden. Daher würde ich empfehlen, stattdessen die Parameterübergabe zu verwenden. Ich würde empfehlen, gehen Sie durch diese http://www.learncpp.com/cpp-tutorial/4-2a-why-global-variables-are-evil/

+0

empfehlen Sie bitte keine Globals. –

Verwandte Themen