Python Forum
Python 3 Global Variable Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 Global Variable Help
#1
Hi, I'm just learning python and I'm still an absolute beginner, but I've decided to try my hand at a choose your own adventure game.
My goal is to to create a new global variable within a function in order to call it while outside of that function.
I've tried a few things but I'm stuck. I introduce the global variable on line 101. My check is the last line of the code.

The game is working fine as of so far, but any information on getting started for my code that is commented out would also be appreciated



def playagain():
    answer = input('Play again? Y or N')
    if answer == "Y":
        print('Goodluck!')
        levelone()
    else:
        print('Goodbye!')


def get_user_input():
    """Prompts the user for its choice and return it."""
    user_input = input('Choose: ').lower().strip()
    return user_input
def replay():
    user_input = input('Play again Yes/No: ').lower().strip()

    if user_input == 'yes':
        print('_' * 100)
        levelone()
    elif user_input == 'no':
        print('thanks for playing!')
        print("That's all for now")
       
        

# class item():
#     def __init__(self, name, description,value)

# class weapon():
# def health():
# class armour():
# class player():
# class inventory():
# def combat():
# class gold():



def levelone():
    waiting_for_input = True
    print('Welcome to your adventure.')
    print('Are you ready? yes/no')
    print('1 for yes')
    print('2 for no')

    while waiting_for_input:
        user_input = get_user_input()
        if user_input == '1':
            print('_'*100)
            print('Alright, Lets Begin')
            break
        elif user_input == '2': 
            print('_'*100)
            print('Maybe later')
            waiting_for_input = False
            break
    while waiting_for_input:
        print('_'*100)
        print('You awaken to find youself in a dimly lit cavern, beside you lies a sword and a shield ')
        print('do you want to pick them up')
        print('1 for yes')
        print('2 for no')
        break
    while waiting_for_input:
        user_input = get_user_input()

        if user_input == '1':
            print('_'*100)
            print('You stand up, dust yourself off while trying to ignore your throbbing head, grab the weapons on the ground,')
            print('look around and see a door ajar.')
            break
        elif user_input == '2':
            print('_'*100)
            print('You stand up, dust yourself off while trying to ignore your throbbing head, ')
            print('look around and see a door ajar.')
            break
    while waiting_for_input:
        print('What shall you do?')
        print('1: Go through the door')
        print('2: Search the room')
        break
    while waiting_for_input:
        user_input = get_user_input()

        if user_input == '1':
            print('_'*20)
            print('As you open the door you see 5 people standing around a long table filled with maps')
            print('in the center of the table stands a tall, burly man, he notices you enter the room and the men fall silent')
            print("Looking up from the maps, he looks and you and says, ' Glad to see you are finally awake, what's your name?")
            your_name = input('your name: ')
            character_name = "Hello, {}, welcome to the Gryphon's den, do you remember anything before you got here?".format(your_name)
            print(character_name)
            break
        elif user_input == '2':
            print('_'*20)
            print('You search the room and find nothing of but some cobwebs and dirt.')
            print('You decide to go through the door and meet your fate.')
            print('As you open the door you see 5 people standing around a long table filled with maps')
            print('in the center of the table stands a tall, burly man. He notices you enter the room and the men fall silent')
            print("Looking up from the maps, he looks and you and says, ' Glad to see you are finally awake, what's your name?")
            global name
            name = input('your name: ')
            line = "Hello, {}, welcome to the Gryphon's den, do you remember from anything before you got here?".format(name)
            print(line)
            break
        



# Start Game

levelone()


replay()


print(name)
Error:
Traceback (most recent call last): File "c:/Users/Tridium/Documents/python/Adventure.py", line 118, in <module> print(name) NameError: name 'name' is not defined
Reply


Messages In This Thread
Python 3 Global Variable Help - by Tridium - Jul-28-2019, 03:24 AM
RE: Python 3 Global Variable Help - by ThomasL - Jul-28-2019, 06:32 AM
RE: Python 3 Global Variable Help - by Tridium - Jul-28-2019, 07:49 AM
RE: Python 3 Global Variable Help - by ThomasL - Jul-28-2019, 06:48 PM
RE: Python 3 Global Variable Help - by Windspar - Jul-28-2019, 01:31 PM
RE: Python 3 Global Variable Help - by Tridium - Jul-29-2019, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Time to Update a Global Variable and Label in TkInter cameron121901 5 4,074 Apr-22-2019, 05:08 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020