I cannot import from another file - python

I am making a soccer game. I am new to Python. I am trying to make soccerplayers on a file, then import it to my main game.
Here is soccerplayer.py
class soccerp:
def __init__(self,overall,name,speed,shoot,ballc,defence):
self.overall = overall
self.name = name
self.speed = speed
self.shoot = shoot
self.ballc = ballc
self.defence = defence
Here is soccerkeeper.py
class soccerk:
def __init__(self,overall,name,dive,reactspeed,reach,jump):
self.overall = overall
self.name = name
self.dive = dive
self.reactspeed = reactspeed
self.reach = reach
self.jump = jump
Here is soccerplayerlist.py
from soccerplayer import soccerp
from soccerkeeper import soccerk
#name overall speed shootingpower ballcontrol defence
david = soccerp("david",114,181,179,183,148)
john = soccerp("john",119,179,185,187,151)
soccerplayers = [david,john]
And here is my game.py
import time
from soccerplayerlist import soccerplayers
#name overall speed shootingpower ballcontrol defence
ovr = [120,124,158,132,109] #will edit as the players overalls
teamovr = round(sum(ovr) / len(ovr))
def start():
print("Please pick your teams name : ")
team = input("> ")
print("")
time.sleep(1)
return team
def menu(team):
print("teams name : " + team)
print("team overall : " + str(teamovr))
def game():
team = start()
#while True:
teamovr = round(sum(ovr) / len(ovr))
menu(team)
print(david.name) #checking if the players were imported from soccerplayerlist.py
game()
when I run the code, It says
NameError: name 'david' is not defined
I think that it didnt import the players, I may be wrong, what am I doing wrong here?

The issue is that you're importing the list and not the values of the list.
If you do
print(soccerplayers[0].name)
You should get the desired result.

The name david is not the one you import it is the list of soccerplayers
what you can do is instead of this line.
print(david.name)
do this:
for player in soccerplayers:
if player.name == "david":
david = player
I personaly recommend on pygame it is a library in python which allows to make high level games.

Related

manipulate class instance

I want to give the second instance of my Player class the remaining marker after the first instance gets one of them.
class Player():
available_markers = ['X', 'O']
num_of_players = 0
player2_marker = ' '
def __init__(self):
self.name = input('Please enter your name : ')
self.marker = ' '
Player.num_of_players += 1
def choose_marker(self):
while Player.num_of_players != 2:
self.marker = input('Choose X or O')
if self.marker == Player.available_markers[0]:
Player.player2_marker == Player.available_markers[-1]
else:
Player.player2_marker == Player.available_markers[0]
else:
self.marker = Player.player2_marke
I would like to accomplish this in the instance method but have went through a lot of code that doesn't quite work.
There are some misunderstandings of object-oriented coding in your code so I'll try to address them in my answer. One of the goals of object-oriented programming is the separation of concerns. If you have some logic about how a game works and you have some logic about how a player works, you don't want the logic for both in the same place intermixed.
All the player really needs to know is what their name, marker and player number are:
class Player():
num_of_players = 0
def __init__(self, name, marker):
Player.num_of_players += 1
self.name = name
self.marker = marker
self.number = Player.num_of_players
print(self.number, self.marker)
Separate from that is how you want to start the game and initialize the players. You could create another class called Game but for now, I'll just do a function:
def start_game():
available_markers = ['X', 'O']
print("Player 1")
name = input('Please enter your name : ')
Let's not trust that the user of the program will enter the right thing:
while True:
marker = input('Choose X or O: ')
We will loop forever and just break out of it if a valid option is chosen:
if marker in available_markers:
break
else:
print("Invalid choice. Please pick again")
player1 = Player(name, marker)
We'll remove that marker from the list so the list just has one element in it now:
available_markers.remove(marker)
print("Player 2")
name = input('Please enter your name : ')
player2 = Player(name, available_markers[0])
start_game()
# Player 1
# Please enter your name : Adam
# Choose X or O: X
# 1 X
# Player 2
# Please enter your name : Joe
# 2 O
Note that I create two separate instances of Player.
Let's talk briefly about class variables vs instant variables. I kept num_of_players as a class variable to track the total number of players (accessible by doing Player.num_of_players or player1.num_of_players both will return that there are 2 total players). I also created another variable number so each player can track what their number is. That's an instance variable and tracked separately for each instance.

NameError: name 'stats' is not defined

i am messing about on python trying out classes but i am getting a name error which i dont know how to fix. it works fine when not in a class but now it is it doesnt work i presume this is a obvious problem , or just the whole thing is wrong, and i now look stupid lol, the other variables are things i havent got round to doing yet so ignore them. (yes the stats are ripped straight from fallout")
import time, random
class User():
def players():
players = 0
def stats(self):
perks = 20
print ("you have 20 perk points to spend")
print(""" you have several choices of what to spend
them in:
STRENGTH - what it says on the tin
PERCEPTION - awareness
ENDURANCE - how long you can endure something
CHARISMA - yet again what it says on the tin
INTELLIGENCE - how smart you are
AGILITY - how much of a slippery bugger you are
LUCK - how lucky you are""")
strength = int(input("What level is your strength?"))
perks = perks - strength
perception = int(input("What level is your perception?"))
perks = perks - perception
endurance = int(input("What level is your endurance?"))
perks = perks - endurance
charisma = int(input("What level is your charisma?"))
perks = perks - charisma
intelligence = int(input("What level is your intelligence?"))
perks = perks - intelligence
agility = int(input("What level is your agility?"))
perks = perks - agility
luck = int(input("What level is your luck?"))
perks = perks - luck
if perks >= 0:
print ("this works")
elif perks <=-1:
print("this also works")
def __init__(self,username,stats):
self.username = username
self.stats = stats
players +=1
story = "on"
while story == "on":
print ("Start of story")
stats()
Welcome to my story
Traceback (most recent call last):
File "C:/Users/----/----/python/----.py", line 45, in <module>
stats()
NameError: name 'stats' is not defined
>>>
your problem is that you have the attribute stats and the method stats you can rename your stats attribute to _stats, and also you need to make an object from User class for example
class User():
def __init__(self,username,stats):
self.username = username
self._stats = stats
...
def stats(self):
...
user = User('test', 1)
story = "on"
while story == "on":
print ("Start of story")
user.stats() # method
user._stats # attribute
stats() is a method of your class User. That means stats() exists within the scope of User, but out of the scope it is not defined.
Then there's another problem: Your __init__ takes an argument with the same name (stats), and then you set stats as an attribute of self. That means you override your method stats() with whatever you pass to the __init__ call (when you set self.stats = stats). You should rename this attribute, maybe to self._stats = stats.
And in your global scope, you have never instantiated your class User. So you could do something like this:
user = User("Jeremy", "whatever your stats are")
story = "on"
while story == "on":
print ("Start of story")
user.stats()
This will resolve the error. However I'm not sure if the code will do what you expect it to do. You define a lot of variables within stats(), like strength etc. Maybe you'd want to define them as attributes to self, so self.strength = ....
You can then access them in the global scope via user.strength etc.

How to call variable from one function in another file inside a function in another file?

I can't really copy over all of the code in both of these files since it is over 1000 lines, but essentially I am making a program that has the variables 'strength', 'endurance', 'strength', 'dexterity', 'intelligence', 'wisdom', and 'luck' (it's an rpg game) and I am trying to get the variable 'damage', which is an equation of 'strength' * 'dexterity' / 100, into another file. All of these variables are within the function character() in a file specifically for creating your character, and I'm trying to call over these variables again in another file for the main game, inside a variable called fight(). I've tried a multitude of things such as global variables, and using return, but nothing has worked for me. I'm sorry if I explained that poorly, comment if you have any questions.
The code in question.
character.py
def character():
#tons of stuff go here
global damage
damage = strength * dexterity / 100
game.py
def fight():
choice = input('(Type in the corresponding number to choose.) ')
global enemy_health
global damage
if choice == 1:
print ' '
print enemy_health,
enemy_health += -damage
print '-->', enemy_health
Thank you for your time.
I guess you could try importing character.py to game.py.
game.py: (edited)
import character
character_health = character.health
character_strength = character.strength
def fight():
...
But yeah, use classes.
Edit: example class
game.py:
class Character(object):
def __init__(self, health, strength):
self.health = health
self.strength = strength
self.alive = True
def check_dead(self):
self.alive = not(self.health)
def fight(self, enemy):
self.health -= enemy.strength
enemy.health -= self.strength
self.check_dead()
enemy.check_dead()
if __name__ == "__main__":
player = Character(300, 10) # health = 300, strength = 10
enemy = Character(50, 5) # health = 50, strength = 5
player.fight(enemy)
print("Player's health: {}\nIs he alive? {}\nEnemy's health: {}\nIs he alive? {}".format(player.health, player.alive, enemy.health, enemy.alive))

Referencing Class properties with variables in Python 2.7

I'm working on a text based game, but I haven't found a very efficient solution to my fighting system yet. I currently have my statements set up sort of like this:
class Weapon1:
damage = 4
price = 5
class Weapon2:
damage = 4
price = 5
class Enemy1:
damage = 2
health = 5
class enemy2:
damage = 3
health = 6
def fight():
Weapon = raw_input("What weapon would you like to use?")
if Weapon == "Weapon 1":
if enemy = "Enemy1":
Enemy1.health -= Weapon1.damage
else:
Enemy2.health -= Weapon1.damage
else:
if enemy = "Enemy1":
pass
else:
pass
I have a lot more enemies and classes than that, so I would like to have a single statement that can reference classes' properties using variables. I decided not to upload the actual function due to it being over 100 lines long.
The pseudocode for what I'm after would look something like this:
class pistol:
damage = 4
price = 5
class cannon:
damage = 4
price = 5
class Enemy1:
damage = 2
health = 5
class enemy2:
damage = 3
health = 6
def fight():
Weapon = raw_input("What weapon would you like to use?")
enemy.health -= weapon.damage
You can use a simple dict, here with namedtuple to make the code easier to read:
from collections import namedtuple
Weapon = namedtuple('Weapon', ['damage', 'price'])
weapons = {
'pistol': Weapon(4, 5),
'cannon': Weapon(4, 5),
# ...
}
def fight():
weapon_name = raw_input("What weapon would you like to use?")
enemy.health -= weapons[weapon_name].damage
make the weapon,enemy class and initiate before doing fight()
class weapon:
def __init__(self,d,p):
self.damage = d
self.price = p
def initiate():
pistol = weapon(4,5)
#....

Python custom modules - error with example code

I am reading the book "Python Programming for the Absolute Beginner (3rd edition)". I am in the chapter introducing custom modules and I believe this may be an error in the coding in the book, because I have checked it 5 or 6 times and matched it exactly.
First we have a custom module games.py
class Player(object):
""" A player for a game. """
def __init__(self, name, score = 0):
self.name = name
self.score = score
def __str__(self):
rep = self.name + ":\t" + str(self.score)
return rep
def ask_yes_no(question):
""" Ask a yes or no question. """
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def ask_number(question, low, high):
""" Ask for a number within a range """
response = None
while response not in range (low, high):
response = int(input(question))
return response
if __name__ == "__main__":
print("You ran this module directly (and did not 'import' it).")
input("\n\nPress the enter key to exit.")
And now the SimpleGame.py
import games, random
print("Welcome to the world's simplest game!\n")
again = None
while again != "n":
players = []
num = games.ask_number(question = "How many players? (2 - 5): ", low = 2, high = 5)
for i in range(num):
name = input("Player name: ")
score = random.randrange(100) + 1
player = games.Player(name, score)
players.append(player)
print("\nHere are the game results:")
for player in players:
print(player)
again = games.ask_yes_no("\nDo you want to play again? (y/n): ")
input("\n\nPress the enter key to exit.")
So this is exactly how the code appears in the book. When I run the program I get the error IndentationError at for i in range(num):. I expected this would happen so I changed it and removed 1 tab or 4 spaces in front of each line from for i in range(num) to again = games.ask_yes_no("\nDo you want to play again? (y/n): ").
After this the output is "Welcome to the world's simplest game!" and that's it.
I was wondering if someone could let me know why this is happening?
Also, the import games module, is recognized in Eclipse after I added the path to PYTHONPATH.
I actually have this book myself. And yes, it is a typo. Here is how to fix it:
# SimpleGame.py
import games, random
print("Welcome to the world's simplest game!\n")
again = None
while again != "n":
players = []
num = games.ask_number(question = "How many players? (2 - 5): ", low = 2, high = 5)
for i in range(num):
name = input("Player name: ")
score = random.randrange(100) + 1
player = games.Player(name, score)
players.append(player)
print("\nHere are the game results:")
for player in players:
print(player)
again = games.ask_yes_no("\nDo you want to play again? (y/n): ")
input("\n\nPress the enter key to exit.")
All I did was indent num 4 spaces and lined it up with the first for-loop.
You have an infinite loop here:
again = None
while again != "n":
players = []
If this is exactly the way it's printed in the book, the book does have an error.
You've got these two lines:
num = games.ask_number(question = "How many players? (2 - 5): ", low = 2, high = 5)
for i in range(num):
The second one is more indented than the first. That's only legal if the first one is a block-introducer like a for or while or if. Since it's not, this is an IndentationError. And that's exactly what Python is telling you.
(It's possible that you've copied things wrong. It's also possible that you're mixing tabs and spaces, so it actually looks right in your editor, but it looks wrong to Python. But if neither of those is true, the book is wrong.)
So, you attempted to fix it by dedenting everything from that for loop on.
But when you do that, only one line is still left under the while loop:
while again != "n":
players = []
There's nothing that can possibly change again to "n", so this will just spin forever, doing nothing, and not moving on to the rest of the program.
So, what you probably want to do is to indent the num = … line to the same level as the for i… line, so both of them (and all the stuff after) ends up inside the while loop.

Categories