Python Tutorial v3.2.5 Lesson 14 - Nested Functions

preview_player
Показать описание
1:24 Click to skip the Introduction to Lesson 14.
10:58 Skip to an important note about using the Python Visualizer.
14:55 Skip directly to the Challenge Program for Lesson 14.

ThePython Visualizer website can be found here:

Lesson 14 discusses nested functions, which is simply calling a user defined function from within another function. The provided example consists of a program that converts temperatures from Fahrenheit to Celsius to Kelvin. The running of this program is then shown in the Python Visualizer from Lesson 13. Finally, a challenge program in which the programmer designs a help system and menu using nested functions is provided.

This is an introductory series of Python tutorials. This course, from start to finish, is designed to help someone who has never programmed before learn the basics of coding in Python. As this series continues, we examine more advanced Python techniques, functions, and methods.

Keep in mind this tutorial is using an older version of Python, v3.2.5. You will need to click on the "View Older Releases" button to use this specific version. Using the newer versions will not be an issue at this point, but when the lesson proceeds to basic graphics, the Pygame Module we'll use does not support v3.3+ at this time.
Рекомендации по теме
Комментарии
Автор

Thanks for the tutorials mate, they well done. We will really appreciate it if they are grouped in a playlist... It wiil be easiier for us

debordus
Автор

How come this doesn't work:
x = 3
def x():
return x
x()

timmellis
Автор

Students : This is simply one function calling another previously defined function. It does not explain the concept of Nested functions. Search elsewhere.

DaveyGuitar
Автор

Here is my effort. Big help from the program debugging tutorial. Thanks Steve.

# Help menu
def weapon_menu():
    """" Display weapon menu when 'help' is requested"""
   
    print("\n\t  *** WEAPON MENU ***")
    print("""
             (B)attleaxe
             (D)agger
             (L)ongsword
             (P)olearm
                       """)
   
# While loop for user weapon selection
def weapon_choice():
    """ User inputs weapon choices """
   
    weapon = None
    while True:
        weapon = input("\nSelect the weapon you'd like to use. (Enter 'H' for help)\n:")

        if weapon == "B" or weapon == "b":
            return "Battleaxe" 
       if weapon == "D" or weapon == "d":
            return "Dagger"      
       if weapon == "L" or weapon == "l":
            return "Longsword"      
       if weapon == "P" or weapon == "p":
            return "Polearm"       
       if weapon == "H" or weapon == "h":
           weapon_menu()          


# Main
def main():
    """ Title and start program """
   
   
    print("*** WELCOME TO THE DUNGEON ***")
   
 
    cont = "Y" or "y"
    while cont == "Y" or cont == "y":
        weapon = weapon_choice()
        print("The adventurer has chosen to wield the %s." %weapon)
       
        cont = input("\nWould you like to continue (Y/N)?\n:") 

   # End program
    print("\nGoodbye")
   
   
# Start program
main()

garrybeck
Автор

def displayIntro():
print("*** Welcome to ***")
print("*** The Dungeon ***")
\n")

def menu():
print("(B)attleaxe")
print("(L)ongarm")
print("(D)agger")
print("(P)olearm")

def selectWeapon():
displayIntro()
weapon = None
while True:
weapon = input("Select the weapon you would like to use(Press ? for help)")

bel = "Battleaxe"
long = "(L)ongarm"
dal = "(D)agger"
pol = "(P)olearm"

if weapon == "B" or weapon == 'b':
return f"The adventurer has use a {bel}"
elif weapon == "L" or weapon == 'l':
return f"The adventurer has use a {long}"
elif weapon == "D" or weapon == 'd':
return f"The adventurer has use a {dal}"
elif weapon == "P" or weapon == 'p':
return f"The adventurer has use a {pol}"
elif weapon == "?":
menu()

playAgain= "yes"
while playAgain == "yes" or playAgain == "y":
result = selectWeapon()
print(result)
playAgain = input("Would you like to continue (Y/N)? \n:")

eti-inyeneibia