-2

I am making a text adventure game:

#modules
import time
import random

#setup
class hero:
    def __init__(self, name, health, attack, defence):
        self.name = Hname
        self.health = Hhealth
        self.attack = Hattack
        self.defence = Hdefence

def start():
    #name
    print("welcome, hero." , end='')
    time.sleep(0.3)
    Hname = input(" And what shall I call you? ")
    print("What an amazing name", Hname+"!")
    time.sleep(1)
    print("Lets continue the setup...")
    time.sleep(1)
    
    #health
    Hhealth = 100
    
    #attack and defence
    x = int(input("in a fight, are you more likly to run at the enemy(1), or think up a plan first(2)"))
    if x == 1:
        Hattack = 100
        Hdefence = 75
    elif x == 2:
        Hattack = 75
        Hdefence = 100
    else:
        print("Invalid selection: please type either 1 OR 2!")
        x = int(input("in a fight, are you more likly to run through the enemy(1), or think up a plan first(2)"))
    Hstats = [Hname, Hhealth, Hattack, Hdefence]
start()
print(start.Hstats)

At the bottom I am trying to print the stats. I tried making a list and puttion all the atributes of the class hero but another thing is how would I get a list from a function?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    You need a return statement to return something from a function. It seems you want start `print(start.Hstats)` to be a method in the class. Check the indentation. – Carl_M Nov 03 '22 at 00:34
  • Though it's not your question, the _init_ method in your hero class is badly defined (you don't use the correct arguments: it should be self.name = name...); later, after the hero stats have been defined in start(), you can create a new hero with new_hero = hero(Hname, ...). – Swifty Nov 03 '22 at 00:51
  • Another problem: else: print("Invalid selection: please type either 1 OR 2!") x = int(input("in a fight, are you more likly to run through the enemy(1), or think up a plan first(2)")) is badly positioned, since x is not used afterwards! It should be put before the test (if x == 1...). See this post on how to correctly catch errors in an input: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Swifty Nov 03 '22 at 00:53

1 Answers1

0

You can't access variables in a function like that. The function needs to return the value.

def start():
    #name
    print("welcome, hero." , end='')
    time.sleep(0.3)
    Hname = input(" And what shall I call you? ")
    print("What an amazing name", Hname+"!")
    time.sleep(1)
    print("Lets continue the setup...")
    time.sleep(1)
    
    #health
    Hhealth = 100
    
    #attack and defence
    x = int(input("in a fight, are you more likly to run at the enemy(1), or think up a plan first(2)"))
    if x == 1:
        Hattack = 100
        Hdefence = 75
    elif x == 2:
        Hattack = 75
        Hdefence = 100
    else:
        print("Invalid selection: please type either 1 OR 2!")
        x = int(input("in a fight, are you more likly to run through the enemy(1), or think up a plan first(2)"))
    return Hname, Hhealth, Hattack, Hdefence

print(start())
Barmar
  • 741,623
  • 53
  • 500
  • 612