How to update the value of class attrubutes in Python? - python

So I have this simple code for a class CoffeeMachine.
The question is: Is there a way to simplify def buy so that instead of subtracting values from each parameter, the value of all parameters will be updated at once? something like
return CoffeeMachine(- 250, - 0, - 16, - 1, + 4)
instead of:
self.water -= 250
self.beans -= 16
self.money += 4
self.cups -= 1
the code is bellow (redundant functions deleted so it will be easier to read)
class CoffeeMachine:
def __init__(self, water, milk, beans, cups, money):
self.water = water
self.milk = milk
self.beans = beans
self.cups = cups
self.money = money
def buy(self):
coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
if coffee_type == '1':
if self.water <= 250:
print('Sorry, not enough water!')
elif self.beans <= 16:
print('Sorry, not enough beans!')
else:
print('I have enough resources, making you a coffee!')
self.water -= 250
self.beans -= 16
self.money += 4
self.cups -= 1
elif coffee_type == '2':
if self.water <= 350:
print('Sorry, not enough water!')
elif self.milk <= 75:
print('Sorry, not enough milk!')
elif self.beans <= 20:
print('Sorry, not enough beans!')
else:
self.water -= 350
self.milk -= 75
self.beans -= 20
self.money += 7
self.cups -= 1
print('I have enough resources, making you a coffee!')
elif coffee_type == '3':
if self.water <= 200:
print('Sorry, not enough water!')
elif self.milk <= 100:
print('Sorry, not enough milk!')
elif self.beans <= 12:
print('Sorry, not enough beans!')
else:
self.water -= 200
self.milk -= 100
self.beans -= 12
self.money += 6
self.cups -= 1
print('I have enough resources, making you a coffee!')
elif coffee_type == 'back':
pass
Coffee_Machine = CoffeeMachine(400, 540, 120, 9, 550)
Coffee_Machine.power_on()

Class attributes are the variables in a class whose value is equal to all the objects/instances of that class. We can change the value of class attribute by class name.
Lets look at the below class x
class x:
a=7
def __init__(self,z):
self.b=z
def sum(self):
return(self.a+self.b)
e=x(3)
f=x(4)
print("Initial sum\n")
print(e.sum(),f.sum())
print("\n")
x.a=5
print("Class attribute has been changed\n")
print("After changing class attribute\n")
print(e.sum(),f.sum())
Output:
Initial sum
10 11
Class attribute has been changed
After changing class attribute
8 9

You could split your buy method logic in different methods that attempt to buy the specific coffee type. And then create a new method that updates the values (which assumes that the values were sufficient):
def buy(self):
coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
if coffee_type == '1':
self.attempt_to_buy_espresso()
# ...
def attempt_to_buy_espresso(self):
price = 4
required_water = 250
required_beans = 16
if self.water <= required_water:
print('Sorry, not enough water!')
elif self.beans <= required_beans:
print('Sorry, not enough beans!')
else:
print('I have enough resources, making you a coffee!')
self.prepare_drink(price=price, water=required_water, beans=required_beans)
def prepare_drink(self, price, water=0, beans=0, milk=0)
self.money += price
self.water -= water
self.beans -= beans
self.milk -= milk
self.cups -= 1
You could improve this further, e.g. by storing the required water, beans and milk values for each drink in an unified data structure (for example a dictionary) so that they are not spread across different methods, or separate the logic of preparing the drink and updating the money value, since those represent separate actions.

Related

Simulation task, I'm stuck with producing correct output

I have been tasked with producing a simple program that simulates the actions of a vehicle from a list of command stored & accessed within a text file.
The expected output would look something like this;
Loading simulation...
Accelerating...
Speed = 5, Gear = 1, Direction = 0
Accelerating...
Speed = 10, Gear = 1, Direction = 0
Accelerating...
Changing up...
Current gear = 2
Speed = 15, Gear = 2, Direction = 0
Accelerating...
Speed = 20, Gear = 2, Direction = 0
Accelerating...
Yet my output looks like this;
Loading Simulation....
Car Gear is First (1)
Accelerating...
Car Gear is First (1)
Speed = 5, Gear = 1, Direction = [-1, 0, 1]
Braking...
Car Gear is First (1)
Speed = 0, Gear = 1, Direction = [-1, 0, 1]
Car Gear is First (1)
Accelerating...
Car Gear is First (1)
Speed = 5, Gear = 1, Direction = [-1, 0, 1]
Braking...
Car Gear is First (1)
Speed = 0, Gear = 1, Direction = [-1, 0, 1]
Car Gear is First (1)
When I run my code in Idle, i get no errors of any kind, when i use Thonny, it gives me the folloing error when it analyses the code:
Line 113 : Either all return statements in a function should return an expression, or none of them should.
Line 113 : Unused argument 'selected_gear'
Below is a copy of my code also:
RIGHT = 1
LEFT = -1
FORWARD = 1
REVERSE = 0
STRAIGHT = 0
#gears and allowable speeds are as follows:
#zero (reverse) (speed -1 to -10). Max reverse speed of car is -10
#one (speed 0 to 10)
#two (speed 10 to 20)
#three (speed 20 to 30)
#four (speed 30 to 45)
#five (speed 45 to 80). Max speed of car is 80
#gears change automatically, one gear at a time
#direction values are similar to numbers on clock face
#0 = 12 = straight on. All other directions = 1-11
class Car:
def __init__(self):
self.speed = 0
self.gear = [0,1,2,3,4,5]
self.direction = [-1,0,1]
self.broken = False #indicates whether car is broken
self.simulation = []
self.simulation_loaded = False
def accelerate(self):
if self.broken:
print("Car is broken!")
return
print("Accelerating...")
if self.gear == REVERSE:
self.speed -= 5
else:
self.speed += 5
if self.speed > 80:
self.speed = 80
if self.speed < -10:
self.speed = -10
self.change_gear(self.gear)
self.display_stats()
def brake(self):
if self.broken:
print("Car is broken...")
return
print("Braking...")
if self.speed < 0:
self.speed += 5
if self.speed > 0:
self.speed = 0
elif self.speed > 0:
self.speed -= 5
if self.speed < 0:
self.speed = 0
self.change_gear(self.gear)
self.display_stats()
def turn_steering_wheel(self, direction_change):
if self.broken:
print("Car is broken...")
return
if self.gear == REVERSE:
print ("Car is in reverse...")
if direction_change == RIGHT:
self.direction = -1
print("Reversing Right")
elif direction_change == REVERSE:
self.direction = 12
print("Reversing")
elif direction_change == LEFT:
self.direction = 1
print("Reversing Left")
elif self.gear == FORWARD:
if direction_change == LEFT:
self.direction = -1
print("Turning Left")
elif direction_change == STRAIGHT:
self.direction = 0
print("Moving Forward")
elif direction_change == RIGHT:
self.direction = 1
print("Turning Right")
self.display_stats()
def change_gear(self, selected_gear = FORWARD):
if self.broken:
print("Car is broken...")
return self.broken
# if self.gear == 0 and self.speed >= 0:
# print("you are going forward while in reverse gear...")
# return self.broken
# elif self.gear >= 1 and self.speed <= -1:
# print("you are going reverse while in foward gear...")
# return self.broken
if self.speed <= -1:
self.gear = 0
print("Car Gear is Neutral (0)")
return self.gear
elif self.speed <= 10:
self.gear = 1
print("Car Gear is First (1)")
return
elif self.speed <= 20:
self.gear = 2
print("Car Gear is Second (2)")
return
elif self.speed <= 30:
self.gear = 3
print("Car Gear is Third (3)")
return
elif self.speed <= 40:
self.gear = 4
print("Car Gear is Fourth (4)")
return
elif self.speed <= 50:
self.gear = 5
print("Car Gear is Fifth (5)")
return
self.display_stats()
self.change_gear(self.gear)
#check to see if car is going forward when reverse is selected and vice versa
#work out what gear you need to be in based on car’s speed
#Loop one gear at a time, either changing up or down, to get to required gear
print("Changing up...")
def display_stats(self):
print(f"Speed = {self.speed}, Gear = {self.gear}, Direction = {self.direction}")
def load_simulation(self, filename):
file = open(filename)
line = file.readline()
while line !='':
self.simulation.append(line.strip())
line = file.readline()
file.close()
self.simulation_loaded = True
return self.simulation
def run_simulation(self):
if self.simulation_loaded == False:
print("Error - Simulation.txt file is present")
else:
print("Loading Simulation....")
for action in self.simulation:
if action == "FORWARD":
self.change_gear (FORWARD)
elif action == "ACCELERATE":
self.accelerate()
elif action == "LEFT":
#self.direction(LEFT)
print("TURNING LEFT")
elif action == "RIGHT":
#self.direction(RIGHT)
print("TURNING RIGHT")
'''***WHEN USING SELF.DIRECTION(LEFT)/(RIGHT) the following error is given:
Traceback (most recent call last):
File "C:\Users\lenovo\Desktop\Bsc Computer Forensics - Laptop\Software-Dev\car-temp-2.py", line 207, in <module>
my_car.run_simulation()
File "C:\Users\lenovo\Desktop\Bsc Computer Forensics - Laptop\Software-Dev\car-temp-2.py", line 183, in run_simulation
self.direction(LEFT)
TypeError: 'list' object is not callable*** '''
elif action == "BRAKE":
self.brake()
else:
self.change_gear (REVERSE)
if __name__ == '__main__':
my_car = Car()
my_car.load_simulation("simulation.txt")
my_car.run_simulation()
Would I please be able to ask if anyone could explain what the errors im getting mean and where in my code I need to be looking, its got me feeling a little lost now - I've tried to research them, fix them but anyhting I try either seems to have no impact or gives errors that otherwise don't exist.
The errors occur in your change_gear() function, although they are more like warnings and should not pose serious problems:
Line 113 : Either all return statements in a function should return an expression, or none of them should.
The first two return statements return a value while the others don't. This is inconsistent and makes the code harder to understand. Since you don't actually use the returned values anywhere, you can remove them (i.e. use plain return).
Line 113 : Unused argument 'selected_gear'
You don't use the selected_gear argument anywhere inside change_gear(). You can remove it to get rid of the warning/error.

Issue with variable defining

When I execute the following code, I get the error "player_normal_defense is not defined". I get what the issue is but if I had to define the variable inside the Defense function, it would be totally pointless since the variable stores the initial defense value for further usage. How should I solve this?
import random
import sys
import os
import time
from Entity import *
class Battle():
def Attack(self, attacker, defender):
damage = attacker.atk - defender.dif
defender.hp -= damage
def Defense(self, owner, defending, entity_id):
defense_boost = 10
if defending:
owner.dif += defense_boost
elif not defending:
if entity_id == 1:
owner.dif = player_normal_defense
elif entity_id == 2:
owner.dif = enemy_normal_defense
def Turn_Generator(self, pl, en):
turns_list = []
speed_product = pl.speed / en.speed
first_speed_product = speed_product
if speed_product >= 1:
turns_list.append("P")
elif speed_product < 1:
turns_list.append("E")
while len(turns_list) < 100:
if speed_product >= 2:
turns_list.append("P")
turns_list.append("P")
speed_product = 0.6
elif speed_product < 2 and speed_product >= 1:
turns_list.append("P")
if first_speed_product <= 0.5:
speed_product = first_speed_product
else:
speed_product = 0.6
elif speed_product <= 0.5:
turns_list.append("E")
turns_list.append("E")
speed_product = 1
elif speed_product > 0.5 and speed_product < 1:
turns_list.append("E")
if first_speed_product >= 2:
speed_product = first_speed_product
else:
speed_product = 1
return turns_list
def Combat(self, player, enemy):
turn_counter = 1
player_normal_defense = player.dif
enemy_normal_defense = enemy.dif
fighting = True
while fighting:
print(f"Turn {turn_counter}")
turns = self.Turn_Generator(player, enemy)
for current_turn in turns:
print(f"""
======================
YOUR HPs: ENEMY HPs:
{player.hp} {enemy.hp}
======================""")
print(f"""
================
Current turn: {current_turn}
================
===========
Next turns:
{turns[1]}
{turns[2]}
{turns[3]}
{turns[4]}
===========
""")
if current_turn == "P":
choice = input("1. Attack\n2. Defend\n")
if choice == "1":
self.Attack(player, enemy)
self.Defense(player, False, 1)
elif choice == "2":
self.Defense(player, True, 1)
else:
print("Lost your chance!")
elif current_turn == "E":
enemy_choice = random.randint(1, 2)
if enemy_choice == 2:
print("He attacks you!")
self.Attack(enemy, player)
self.Defense(enemy, False, 2)
else:
print("He defends himself.")
self.Defense(enemy, True, 2)
time.sleep(3)
os.system("clear")
turns.pop(0)
if player.hp <= 0:
print("You died!")
sys.exit(0)
elif enemy.hp <= 0:
print("YOU WON!")
fighting = False
break
turn_counter += 1
When entity is defending, instead of running a Defence function to add a bonus to entitys Defence score only if it is true, I would just not run the Defence function when attacking. When entity is defending then you can run the Defense (Bonus) function as;
def Defending():
defence_boost = 10
in the decision check;
if choice == "2":
self.dif += Defending()
One issue I believe you will have with the code is that you are raising the entitys .dif stat with each occurrence of defending. This means that when this has cycled multiple times the base dif may be too high for any attackers damage to do anything to the opponents health.

Calling a class function outside of the function it was declared

I'm trying to call a class function player.Decide() in another function where it wasn't declared. Im getting the error 'player is not defined. How would I go about fixing this?
def BattleLogic():
global enemy, enemy_race, name, level, strength, dexterity, cunning, enemy_speed, player_speed, type_speed
global move, turn, prefix
if enemy in ['deer']:
enemy_race = 'animal'
if enemy_race == 'animal':
chance = random.randint(1,1000)
if chance <= 10:
prefix = 'crippled'
elif chance > 10 and chance <= 50:
prefix = 'old'
elif chance >50 and chance <= 250:
prefix = 'young'
elif chance > 250 and chance <= 750:
prefix = None
elif chance > 750 and chance <= 950:
prefix = 'strong'
elif chance > 950 and chance <= 990:
prefix = 'alpha'
elif chance > 990 and chance <= 999:
prefix = 'possessed'
elif chance == 1000:
prefix = '*CONVERTED*'
else:
prefix = 'error'
opponent = Enemy(str(prefix),str(enemy),str(enemy_race))
player = Player(str(name),level,strength,dexterity,cunning)
player.Initiative()
opponent.Initiative()
if enemy_speed > player_speed:
move = 0
elif player_speed > enemy_speed:
move = 1
else:
move = random.randint(0,1)
turn = 0
Battle()
def Battle():
global turn, move, prefix, enemy, type_speed, enemy_title
if turn == 0:
print('\n\n')
if prefix == None:
enemy_title = enemy.capitalize()
else:
enemy_title = prefix.capitalize()+' '+enemy.capitalize()
SlowPrint('A '+enemy_title+' gets into position for battle',type_speed,0.5)
if move == 1:
SlowPrint(enemy_title+' makes the first move',type_speed,0.25)
else:
SlowPrint('You make the first move',type_speed,0.25)
if move == 0:
turn += 1
move = 1
player.Decide()
else:
turn += 1
move = 0
opponent.Decide()
Just pass a player object into the function. The you are free to use player.Decide().
The major problems with your coding, which is my personal opinion, is that you do not have the habit of passing something into the function, which as a result forces you to declare too many global variables.
A simple example (in your case, you want the object to be a player)
def Battle(obj):
return obj.Decide()

Python Simple American Football Simulator Not Running

Simple football simulation for my class project. I simplified a lot of the rules and aspects of the game, so everything isn't very accurate compared to an actual football game. However, When I try running my function, only the line of code which prints the name of the QB throwing to the receiver prints. For example when I run it only, "Jared Goff throws to Brandin Cooks for 33 yards!" shows up in the display. How can I get the whole function to run/print? Not sure where I went wrong.
import random
rams_qb = ["Jared Goff"]
patriots_qb = ["Tom Brady"]
rams_receivers = ["Cooper Kupp", "Brandin Cooks"]
patriots_receivers = ["Julian Edelman", "Josh Gordon"]
rams_score = 0
patriots_score = 0
quarter_time = 900
def remaining_time():
global quarter_time
global rams_score
global patriots_score
if quarter_time > 0:
if random.randint(0,100) < 50:
return rams_possesion()
else:
return patriots_possesion()
elif quarter_time == 0:
if rams_score > patriots_score:
print ("Rams Win!")
else:
print ("Patriots Win!")
def rams_possesion():
global quarter_time
global rams_score
rams_ball_position = 50
rams_downs = 1
if rams_ball_position == rams_ball_position + 10:
rams_downs = 1
else:
rams_downs += 1
if rams_ball_position == 100:
rams_score == rams_score + 6
print ("RAMS TOUCHDOWN!")
return rams_fieldgoal
if rams_downs <= 4:
rams_yardage_gained = random.randint(0,50)
print ((random.choice(rams_qb)),("throws to"),
(random.choice(rams_receivers)),("for"),(str(rams_yardage_gained)),
("yards!"))
rams_ball_position == rams_ball_position + rams_yardage_gained
quarter_time -= random.randint(0,30)
if rams_downs >= 5:
return patriots_possesion
def rams_fieldgoal():
global rams_score
if random.randint(0,100) < 83:
rams_score == rams_score + 1
print ("RAMS SCORE FIELDGOAL!")
else:
print ("RAMS MISS FIELDGOAL")
return patriots_possesion
def patriots_possesion():
global patriots_score
patriots_ball_position = 50
patriots_downs = 1
if patriots_ball_position == patriots_ball_position + 10:
patriots_downs = 1
else:
patriots_downs += 1
if patriots_ball_position == 100:
patriots_score == patriots_score + 6
print ("PATRIOTS TOUCHDOWN!")
return patriots_fieldgoal
if patriots_downs <= 4:
patriots_yardage_gained = random.randint(0,50)
print ((random.choice(patriots_qb)),("throws to"),
(random.choice(patriots_receivers)),("for"),(str(patriots_yardage_gained)),
("yards!"))
patriots_ball_position == patriots_ball_position +
patriots_yardage_gained
if patriots_downs >= 5:
return rams_possesion()
def patriots_fieldgoal():
global patriots_score
if random.randint(0,100) < 87:
patriots_score == patriots_score + 1
print ("PATRIOTS SCORE FIELDGOAL!")
else:
print ("PATRIOTS MISS FIELDGOAL")
return rams_possesion()
remaining_time()
My best guess here is that your comparisons are not taking into account the fact that the values can go over the target. For example, when checking yardage, you're checking for equality to 100, but you aren't guaranteeing that the value won't go over 100. You have a similar bug with checking if the quarter_time == 0 when it should be <= 0 to make sure it triggers.
I can't tell you if changing these comparisons will fix your program, but try these changes and comment how the behavior changes.

Why is my variable not changing its values?

I have been making a text-based game. I have gotten to a point where I am trying to do a battle. I have the enemy's hp going down but when I hit it again, the enemy's hp is back to what I had originally set it as. I hope these help.
while do != 'hit rat with sword' or 'run away':
enemyhp= 50
enemyattack= [0,1,3]
OldSwordattack= [0,1,4]
print('What do you do?(attack or run away)')
do=input()
if do == 'run away':
print('You can\'t leave mom like that!')
if do == 'hit rat with sword':
hit= random.choice(OldSwordattack)
if hit == OldSwordattack[0]:
print('Your swing missed')
print('Enemy\'s HP=' + str(enemyhp))
if hit == OldSwordattack[1]:
print('Your swing hit, but very lightly.')
enemyhp= enemyhp - 1
print('Enemy\'s HP=' + str(enemyhp))
if hit == OldSwordattack[2]:
print('Your swing hit head on!')
enemyhp= enemyhp - 4
print('Enemy\'s HP=' + str(enemyhp))
>In front of mom you see a giant, yellow-teethed rat.
>What do you do?(attack or run away)
>hit rat with sword
>Your swing hit, but very lightly.
>Enemy's HP=49
>What do you do?(attack or run away)
>hit rat with sword
>Your swing hit, but very lightly.
>Enemy's HP=49
>What do you do?(attack or run away)
You see? The above is the program running. I do not see why the value is not being changed.
because you are initializing it at the beginning of the iteration. So just move the
enemyhp= 50
before the while loop, like this
enemyhp= 50
while do != 'hit rat with sword' or 'run away':
Just for fun I've extended your example program to include some more advanced language constructs. May you find it entertaining: (Python 3)
import random
import re
def dice(s, reg=re.compile('(\d+d\d+|\+|\-|\d+)')):
"""
Executes D+D-style dice rolls, ie '2d6 + 3'
"""
total = 0
sign = 1
for symbol in reg.findall(s):
if symbol == '+':
sign = 1
elif symbol == '-':
sign = -1
elif 'd' in symbol:
d,s = [int(k) for k in symbol.split('d')]
for i in range(d):
total += sign * random.randint(1, s)
else:
total += sign * int(symbol)
return total
class Character():
def __init__(self, name, attack, defense, health, weapon):
self.name = name
self.attack = attack
self.defense = defense
self.health = health
self.weapon = weapon
def hit(self, target):
if self.is_dead:
print('{} is trying to become a zombie!'.format(self.name))
return 0
attack = self.attack + self.weapon.attack_bonus + dice('2d6')
defense = target.defense + dice('2d6')
if attack > defense:
damage = int(random.random() * min(attack - defense + 1, self.weapon.damage + 1))
target.health -= damage
print('{} does {} damage to {} with {}'.format(self.name, damage, target.name, self.weapon.name))
return damage
else:
print('{} misses {}'.format(self.name, target.name))
return 0
#property
def is_dead(self):
return self.health <= 0
class Weapon():
def __init__(self, name, attack_bonus, damage):
self.name = name
self.attack_bonus = attack_bonus
self.damage = damage
def main():
name = input("So, what's your name? ")
me = Character(name, 4, 6, 60, Weapon('Old Sword', 2, 4))
mom = Character('Mom', 2, 2, 50, Weapon('Broom', 0, 1))
rat = Character('Crazed Rat', 5, 2, 40, Weapon('Teeth', 1, 2))
print("You are helping your mother clean the basement when a Crazed Rat attacks. "
"You grab your Grandfather's sword from the wall and leap to defend her!")
actors = {me, mom, rat}
coward = False
killer = None
while (me in actors or mom in actors) and rat in actors:
x = random.choice(list(actors))
if x is mom:
mom.hit(rat)
if rat.is_dead:
killer = mom
actors -= {rat}
elif x is rat:
target = random.choice(list(actors - {rat}))
rat.hit(target)
if target.is_dead:
actors -= {target}
if target is mom:
print('Your mother crumples to the floor. AARGH! This rat must die!')
me.attack += 2
else:
print('Well... this is awkward. Looks like Mom will have to finish the job alone...')
else: # your turn!
print('Me: {}, Mom: {}, Rat: {}'.format(me.health, 0 if mom.is_dead else mom.health, rat.health))
do = input('What will you do? [hit] the rat, [run] away, or [swap] weapons with Mom? ')
if do == 'hit':
me.hit(rat)
if rat.is_dead:
killer = me
actors -= {rat}
elif do == 'run':
if me.health < 20:
actors -= {me}
coward = True
else:
print("Don't be such a baby! Get that rat!")
elif do == 'swap':
me.weapon, mom.weapon = mom.weapon, me.weapon
print('You are now armed with the {}'.format(me.weapon.name))
else:
print('Stop wasting time!')
if rat.is_dead:
if mom.is_dead:
print('The rat is gone - but at a terrible cost.')
elif me.is_dead:
print("Your mother buries you with your trusty sword by your side and the rat at your feet.")
elif coward:
print("The rat is gone - but you'd better keep running!")
elif killer is me:
print("Hurrah! Your mother is very impressed, and treats you to a nice cup of tea.")
else:
print('Your mother kills the rat! Treat her to a nice cup of tea before finishing the basement.')
else:
print('I, for one, hail our new rat overlords.')
if __name__=="__main__":
main()

Categories