2017-08-02 2 views
-6

Ich bin ein relativer Neuling und versuche mir zu helfen, Funktionen besser zu verstehen. Ich habe eine Übung für mich erstelltAufruf von 2 Funktionen innerhalb einer Funktion

ein einfaches Programm, schreiben Sie an:

  1. Frage 3 einfachen Qs
  2. Respond zu jedem Q je nachdem, welche Benutzereingaben (jeweils Q Antwort durch einen von 3 einzigartigen bestimmt wird Funktionen)
  3. diese Antworten in eine einfache Zusammenfassung Satz am Ende
Put

ich eine Funktion als Haupt ‚Controller‘ bin mit ->Fragen()

Ich möchte Fragen aufrufen() und dann von innen - rufen Sie die anderen 3 Funktionen. Ich habe das Gefühl, meine Funktionen Argumente brauchen - aber ich bin nicht sicher, wie (ich habe versucht, Argumente in den verschiedenen Funktionen setzen - aber vollständig steckengeblieben - Fehler zurückkehrt (siehe unten))

Mein Code:

def naming(): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def age(): # function to respond to age as per user input 
    if in_age > 60: 
     print 'That\'s old!!' 
    elif in_age < 15: 
     print 'That\'s young!!' 
    else: 
     print 'That\'s neither young nor old!!' 

def loc(): # function to respond to location as per user input 
    if in_loc == 'London': 
     print 'London is a big city!!' 
    elif in_loc == 'Manchester': 
     print 'Manchester is a wonderful place!!' 
    else: 
     print 'That sounds OK!!' 


def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming() 
    in_age = input('How old are you? -->') 
    age() 
    in_loc = raw_input('Where do you live? -->') 
    loc() 
    print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 

questions() 

ich vermute, dass in den wichtigsten Fragen() Funktion - ich brauche im

Funktionen Benennung/Alter/loc irgendeine Form von Unterricht oder Argumente liefern würde wirklich etwas Hilfe hier schätzen ! Ja - es gibt ein paar andere ähnliche Themen hier - aber ich habe sie gelesen und keiner ergibt Sinn für mich. Idealerweise - die hilfreichste Sache für mich wäre, wenn ein guter Samariter 3 oder 4 Minuten damit verbringen könnte, meinen Code zu bearbeiten, um korrekt zu funktionieren.

Vielen Dank im Voraus!

PS - hier ist der Screenshot des Fehlers Ich erhalte error

Antwort

0

Eine Variable, die innerhalb einer Funktion definiert ist, ist lokal für diese Funktion. Es ist von dem Punkt aus zugänglich, an dem es bis zum Ende der Funktion definiert ist, und existiert so lange, wie die Funktion ausgeführt wird.

  1. können Sie die Werte als Argumente für andere Funktionen

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David': 
         print 'That\'s a cool name!!' 
        elif in_name == 'Jane': 
         print 'That\'s a great name!!' 
        else: 
         print 'That\'s an OK name!!!' 
    
    def age(in_age): # function to respond to age as per user input 
        if in_age > 60: 
         print 'That\'s old!!' 
        elif in_age < 15: 
         print 'That\'s young!!' 
        else: 
         print 'That\'s neither young nor old!!' 
    
    def loc(in_loc): # function to respond to location as per user input 
        if in_loc == 'London': 
         print 'London is a big city!!' 
        elif in_loc == 'Manchester': 
         print 'Manchester is a wonderful place!!' 
        else: 
         print 'That sounds OK!!' 
    
    
    def questions(): #function to own the whole process (name + age + loc) 
        in_name = raw_input('What is your name? -->') 
        naming(in_name) 
        in_age = input('How old are you? -->') 
        age(in_age) 
        in_loc = raw_input('Where do you live? -->') 
        loc(in_loc) 
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 
    
    questions() 
    
  2. sonst passieren Sie die Variablen als global definieren können, so dass Sie es als Argument nicht übergeben. (diese Methode wird nicht empfohlen)

    def naming(): # function to respond to name as per user input 
        if in_name == 'David': 
         print 'That\'s a cool name!!' 
        elif in_name == 'Jane': 
         print 'That\'s a great name!!' 
        else: 
         print 'That\'s an OK name!!!' 
    
    def age(): # function to respond to age as per user input 
        if in_age > 60: 
         print 'That\'s old!!' 
        elif in_age < 15: 
         print 'That\'s young!!' 
        else: 
         print 'That\'s neither young nor old!!' 
    
    def loc(): # function to respond to location as per user input 
        if in_loc == 'London': 
         print 'London is a big city!!' 
        elif in_loc == 'Manchester': 
         print 'Manchester is a wonderful place!!' 
        else: 
         print 'That sounds OK!!' 
    
    
    def questions(): #function to own the whole process (name + age + loc) 
        global in_name, in_age, in_loc 
        in_name = raw_input('What is your name? -->') 
        naming() 
        in_age = input('How old are you? -->') 
        age() 
        in_loc = raw_input('Where do you live? -->') 
        loc() 
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 
    
    questions() 
    
+0

Brilliant! Vielen Dank dafür. Eine wirklich hilfreiche Antwort - ich war fast selbst dort - aber konnte nicht weiter kommen. Also nochmal danke – JasonC

0

Variable in der Funktion questions() nicht auf die anderen Funktionen bekannt (oder deren Anwendungsbereich auf questions() Funktion beschränkt), das ist, warum Sie bekommen Fehler, nicht definiert

def naming(in_name): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def age(in_age): # function to respond to age as per user input 
    if in_age > 60: 
     print 'That\'s old!!' 
    elif in_age < 15: 
     print 'That\'s young!!' 
    else: 
     print 'That\'s neither young nor old!!' 

def loc(in_loc): # function to respond to location as per user input 
    if in_loc == 'London': 
     print 'London is a big city!!' 
    elif in_loc == 'Manchester': 
     print 'Manchester is a wonderful place!!' 
    else: 
     print 'That sounds OK!!' 


def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming(in_name) 
    in_age = input('How old are you? -->') 
    age(in_age) 
    in_loc = raw_input('Where do you live? -->') 
    loc(in_loc) 
    print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 

questions() 
+0

Vielen dank für diese - das alles macht sen Sei jetzt zu mir. Sehr hilfreich – JasonC

0

besseren Weg ist, sie als Argumente an die Funktionen zu übergeben.

def naming(in_name): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming(in_name) 

Wieder Variable in_age, müssen es int als raw_input konvertieren ist standardmäßig in string Format.

+0

Ich downwoted, weil der Rat bezüglich der globalen Definition ist ein schlechter, imo. Es löst den 'NameError', löst aber das Problem nicht wirklich (Übergabe einer Variable als Argument an eine Funktion) und generell sollte die Verwendung solcher globalen Bereiche vermieden werden (wenn Variable keine Konfigurationskonstante ist). –

+0

@Robin. Ja, es ist wahr. Ich habe den Code vorher nicht getestet. Dieser Teil wurde entfernt. – SunilT

Verwandte Themen