0

I have the following code:

import options
import random


class Player():
    def __init__(self):
        self.name = None
        self.gold = 100
        self.maxhealth = 100
        self.health = self.maxhealth
        self.level = 1
        self.exp = 0
        self.levelUp = 50
        self.gainedexp = self.levelUp - self.exp


    def get_name(self):
        self.name = input("Hey there, traveller! What's your name?\n~~>")
        print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name))

    def gold_counter(self):
        print("You currently have {} gold!".format(player.gold))


class Dragon():
    def __init__(self):
        self.name = "Dragon"
        self.dropgold = random.randint(13,20)
        self.minexp = int(15 * round(player.level * 1.5))
        self.maxexp = int(30 * round(player.level * 1.5))  
        self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp))
        self.maxhealth = 80
        self.health = self.maxhealth




def intro():
    wrong_input = 0
    nar_name = "Narrator"
    print("{}: Uhhhm...".format(nar_name))
    print("{}: Let me check my list...".format(nar_name))
    print("{0}: Ah! Yes! {1}, that's right. I heard you were supposed to be arriving today.".format(nar_name, player.name))

I am also using two other modules, but I'm 99% sure they don't affect this. I get the following output:

Hey there, traveller! What's your name?
~~>Savage Potato
Since you are new around here, 100 gold doubloons have been given to you, Savage Potato!
Do you want to see your balance?
~~> Yes
You currently have 100 gold.
Narrator: Uhhhm...
Narrator: Let me check my list...
Narrator: Ah! Yes! None, that's right. I heard you were supposed to be arriving today.

In the last line, it is printing out the Narrator's name, but not the user's inputted name. I also looked at the python documents on their website, but I couldn't find a fix. Any ideas on how I could stop it from outputting None as the user's name?

EDIT #1: I have player = Player() written later in the module.

EDIT #2: This is all the code I used:

Module 1 (main.py)

import prints
import random

class Player():
    def __init__(self):
        self.name = None
        self.gold = 100
        self.maxhealth = 100
        self.health = self.maxhealth
        self.level = 1
        self.exp = 0
        self.levelUp = 50
        self.gainedexp = self.levelUp - self.exp


    def get_name(self):
        self.name = input("Hey there, traveller! What's your name?\n~~>")
        print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name))




class Dragon():
    def __init__(self):
        self.name = "Dragon"
        self.dropgold = random.randint(13,20)
        self.minexp = int(15 * round(player.level * 1.5))
        self.maxexp = int(30 * round(player.level * 1.5))  
        self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp))
        self.maxhealth = 80
        self.health = self.maxhealth




#while player.exp >= player.levelUp:
    #player.levelUp += 1
    #player.exp = player.exp - player.levelUp
    #player.levelUp = round(player.levelUp * 1.5)
    #print("Congrats! You just levelled up to level {} by gaining {} experience!".format(player.level, player.gainedexp))


def start():
    player.get_name()
    prints.gold_counter()
    prints.intro()
    prints.encounter()





player = Player()    


start()

Module 2 (prints.py)

import options
import random


class Player():
    def __init__(self):
        self.name = None
        self.gold = 100
        self.maxhealth = 100
        self.health = self.maxhealth
        self.level = 1
        self.exp = 0
        self.levelUp = 50
        self.gainedexp = self.levelUp - self.exp


    def get_name(self):
        self.name = input("Hey there, traveller! What's your name?\n~~>")
        print("Since you are new around here, 100 gold doubloons have been given to you, {}!".format(self.name))

    def gold_counter(self):
        print("You currently have {} gold!".format(player.gold))


class Dragon():
    def __init__(self):
        self.name = "Dragon"
        self.dropgold = random.randint(13,20)
        self.minexp = int(15 * round(player.level * 1.5))
        self.maxexp = int(30 * round(player.level * 1.5))  
        self.expgain = random.randint({}, {}.format(self.minexp, self.maxexp))
        self.maxhealth = 80
        self.health = self.maxhealth




def intro():
    wrong_input = 0
    nar_name = "Narrator"
    print("{}: Uhhhm...".format(nar_name))
    print("{}: Let me check my list...".format(nar_name))
    print("{0}: Ah! Yes! {1}, that's right. I heard you were supposed to be arriving today.".format(nar_name, player.name))
    print("{}: Welcome to... THE DRAGON FIGHTER GAME!".format(nar_name))
    print("{}: I know, it isn't the most imaginative name.".format(nar_name))
    print("{}: Don't look at me like that, I tried my hardest!".format(nar_name))
    print("{}: Anyhoo, let's carry on.".format(nar_name))
    print("{}: For some stupid reason, the creator of this game didn't give me an actual name, so\nmy name is just \"Narrator\" or \"N\", but you can call me Larry.".format(nar_name))
    while True:
        option = input("Narrator: Actually, which name would you prefer to call me?\n").upper()
        if option in options.nar_larry_opt:
            nar_name = "Larry"
        elif option in options.nar_narrator_opt:
            nar_name = "Narrator"
            while True:
                ask = input("{}: Maybe \"N\" for short?".format(nar_name)).upper()
                if ask in options.inp_yes_opt:
                    nar_name = "N"
                elif ask in options.inp_no_opt:
                    break
                else:
                    wrong_input += 1
                    if wrong_input == 1:  
                        print("Please try again.")
                    elif wrong_input == 2:
                        print("Try to not put the same thing in next time.")
                    elif wrong_input == 3:
                        print("This isn't funny.")
                    elif wrong_input == 4:
                        print("Seriously.")
                    elif wrong_input == 5:
                        print("OKAY! THIS IS IT! GO BACK TO THE BEGINNING!")
                        intro()

                    continue
                break
        else:
            print("Please try again.")
            continue
        break
    print("{}: So, as I was saying, this game is basically just some dragon quest thingy.".format(nar_name))
    print("{}: You'll probably get tips from me every now and again if I can be bothered.".format(nar_name))
    print("{}: I'll get an test encounter ready.".format(nar_name))



def gold_counter():
    while True:
        option = input("Do you want to see your balance?\n~~> ").upper()
        if option in options.inp_yes_opt:
            print("You currently have {} gold.".format(player.gold))
        elif option in options.inp_no_opt:
            print("You can check your balance later in the game.")
        else:
            print("Please try again.")
            continue
        break



def encounter():
    while True:
        dragon_appear = random.randint(1,2)
        if dragon_appear == 1:
            print("What's that? Looks like a huge bir... \nA DRAGON! A MAJESTIC DRAGON JUST FLEW DOWN FROM THE SKY!")
        else:
            print("What's that? Looks like a huge bir... \n Yeah. Just a giganta-bird.")

    while encounter().dragon_appear != 2:
        print("So that's the message you'll get when a dragon appears.")
        print("And you will be prompted whether you want to run or fight, like so:")
        while True:
            wrong_input = 0
            ask = input("Run away like a coward, or fight the majestic beast?")
            if ask in options.enc_run_opt:
                escape = random.randint(1,2)
                if escape == 1:
                    print("You managed to get away!")
                else:
                    print("You didn't get away. Better luck next time!")
            elif ask in options.enc_attack_opt:
                pass
            else:
                wrong_input += 1
                if wrong_input == 1:  
                    print("Please try again.")
                elif wrong_input == 2:
                    print("Try to not put the same thing in next time.")
                elif wrong_input == 3:
                    print("This isn't funny.")
                elif wrong_input == 4:
                    print("Seriously.")
                continue
            break




player = Player() 

Module 3 (options.py)

inp_yes_opt = {"Y", "YE", "YES", "YEAH", "PLEASE", "YES PLEASE"}
inp_no_opt = {"N", "NO", "NOPE", "NAH"}

nar_larry_opt = {"LARRY", "LARR", "LAR", "LA", "L", "LARRY PLEASE"}
nar_narrator_opt = {"NARRATOR", "NARR", "N", "NAR", "NARRATE", "NOT LARRY"}

enc_run_opt = {"RUN", "RU", "R", "SCRAM", "RUN AWAY", "RUUUUN"}
enc_attack_opt = {"ATTACK", "ATTAK", "A", "FIGHT", "F", "ATTACK", ""}
3
  • It seems you are not using the same instance of Player, try to create it into intro function, it should work Commented Jul 4, 2017 at 19:00
  • I'll try it, and I'll get back to you. I just thought it would've been simpler to just make it into a class and then call it when I need to. It just makes more sense in my head to have it in the class.. Commented Jul 4, 2017 at 19:46
  • 1
    You should see the visibility of your object. If it's global it works because every function has access to it, but if it's local for example you've put it into a function or in another class it won't keep memory of the changes because you are referring to another object. Commented Jul 4, 2017 at 20:18

1 Answer 1

4

If you want to print out the name of the player , you need to pass in the player object to the intro function as a parameter. That assumes intro is not capturing the player object and the player object is not global

At the moment , it seems there is no player object accessible to the scope of the function which is why it outputs None

Sign up to request clarification or add additional context in comments.

3 Comments

Could you share the code that calls the intro function ?
I'm imagining if this function is defined at the very top, it's not able to capture the player instance like you are intending so you should pass in player as a parameter and it will solve your issue
Yeah, just added all the code that I'm currently using. I can't really seem to spot much wrong with it. I have also tried to call the class(es) from main.py into prints.py by doing from main import Player(), from main import class Player(), from main import *, import main, and none of these work, but what I'm doing now is fine, so I'll fix it at a later date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.