Python Threading Issue when starting multiple threads - python

I would like to start 4 independent threads which are basically methods of a class.
What am I doing wrong in the code below:
from threading import Thread
import time
import random
class Creature:
def __init__(self, name):
self.name = name
def melee(self, level):
self.melee = level
def shielding(self, level):
self.shielding = level
def health(self, total):
self.health = total
def attack(self, attacker, opponent):
while 0 != 1:
power = random.randint(1, attacker.melee)
resistance = random.randint(1, opponent.shielding)
resultant = power - resistance
if resistance > 0:
opponent.health -= resistance
if opponent.health < 0:
print(opponent.name, " is dead")
print("Opponent's health ", opponent.health)
quit()
else:
print(attacker.name, " delivered an attack of ", resistance)
print("Opponent's health ", opponent.health)
def healing(self, healed):
while 0 != 1:
if healed.health <= 0:
if healed.health < 50:
life = random.randint(1, 50)
healed.health += life
if healed.health > 100:
healed.health = 100
print(healed.name, " has healed and now has a health of ", self.health)
Monster = Creature("Wasp")
Monster.health = 100
Monster.melee = 30
Monster.shielding = 15
Player = Creature("Knight")
Player.health = 100
Player.melee = 25
Player.shielding = 20
t1 = Thread(target=Player.attack(Monster, Player))
t1.start()
t2 = Thread(target=Monster.attack(Player, Monster),)
t2.start()
t3 = Thread(target=Player.healing(Player), args=())
t3.start()
t4 = Thread(target=Monster.healing(Monster), args=())
t4.start()
Somehow I am doing something wrong since only the t1 gets started.
Why does the program start only t1? :-(
Thanks!

The problem(s) in your code are all demonstrable in this one line:
t1 = Thread(target=Player.attack(Monster, Player))
The trouble is that you're calling Player.attack, not passing it to the Thread constructor. Your attack method never returns, so you never get past the attempt to create the first Thread. You'd want to do something like this:
t1 = Thread(target = Player.attack, args = (Monster, Player))

Related

Syncronization issue between two running threads

This is a partial solution to the single lane bridge problem that says that only cars that move at the same direction can be on the bridge at the same time. So it makes sense since it a single lane bridge that a car moving from left to right, should not be on the bridge at the same time that another car is moving from right to left. While Ive used locks and queues (one for leftbound and one for right bound traffic), cars that move at opposite directions end up crossing the bridge at the same time, and I don't know why because the locks and the logic ardound them should prevent that from happening.
I was hopping that the logic I have added inside the cross_bridge() method would ensure that cars moving towards the opposite direction do not end up on the bridge at the same time.
This is my code up to this point:
import threading, queue
from threading import Lock
from random import randint
import random
from random import choice
from string import ascii_uppercase
import time
q = queue.Queue(3)
ql = queue.Queue(3)
carsList = []
cnt = 0
cntL = 0
lc = []
rc = []
has_lock_a = -1
has_lock_b = -1
has_lock_c = -1
class Car:
def __init__(self, t_arrive, direction, name, idx):
self.t_arrive = t_arrive
self.direction = direction
self.name = name
self.idx = idx
self.currDir = 0
def __str__(self):
return "Car(name: " + self.name + ", time: " + str(self.t_arrive) + ", direction: " + str(self.direction) + ")\n"
class Bridge:
def __init__(self):
self.threads = []
self.threadCnt = 0
self.cntL = 0
self.cnt = 0
self.rc_nt = 0
self.lock_a = Lock()
self.lock_b = Lock()
self.lock_c = Lock()
self.sem = threading.Semaphore(0)
self.capacity = 3
self.dir = -1
def workerR(self):
while True:
itemR = q.get()
print(itemR)
if rc:
index = rc.pop(0)
self.arrive_bridge(carsList[index], index)
while self.dir != -1 or self.dir == 1:
print("hs")
pass
self.lock_a.acquire()
self.cross_bridge(carsList[index], index)
self.exit_bridge(carsList[index])
self.rc_nt += 1
q.task_done()
self.sem.release()
def workerL(self):
while True:
self.sem.acquire()
itemL = ql.get()
print(itemL)
if lc:
index = lc.pop(0)
self.arrive_bridge(carsList[index], index)
while self.dir != -1 or self.dir == 0:
pass
self.lock_a.acquire()
self.cntL += 1
self.cross_bridge(carsList[index], index)
self.exit_bridge(carsList[index])
ql.task_done()
self.sem.release()
def arrive_bridge(self, car: Car, cnt):
print(car.name + " arrived at bridge\n")
def cross_bridge(self, car: Car, cnt):
lock_ac = 0
print(car.name + " crossing bridge\n")
oneFourth = car.t_arrive / 4
for i in range(4):
if i == 2:
self.lock_b.acquire()
self.dir = car.direction
self.lock_a.release()
time.sleep(oneFourth)
self.lock_b.release()
# time.sleep(car.t_arrive)
def exit_bridge(self, car: Car):
print(car.name + " Exiting bridge\n")
self.dir = -1
if __name__ == "__main__":
# first (i), (ii), (iii)
totalNumberOfCars = randint(8, 11)
id = 0
for i in range(totalNumberOfCars):
direction = randint(0, 1)
time3 = randint(5, 7)
car = Car(time3, direction, ''.join(choice(ascii_uppercase) for z in range(4)), id)
id += 1
carsList.append(car)
random.shuffle(carsList)
car_cnt = 0
for car in carsList:
car.idx = car_cnt
car_cnt+=1
# turn-on the worker thread
bridge = Bridge()
threading.Thread(target=bridge.workerR, daemon=True).start()
# threading.Thread(target=bridge.workerR2, daemon=True).start()
threading.Thread(target=bridge.workerL, daemon=True).start()
# send thirty task requests to the worker
for item in range(len(carsList)):
if carsList[item].direction == 1:
q.put(carsList[item])
rc.append(item)
else:
ql.put(carsList[item])
lc.append(item)
# block until all tasks are done
q.join()
print("-------------. 2. > " + str(len(carsList)))
print('All work completed')
OUTPUT (and the error):
Car(name: PJJG, time: 5, direction: 1)
PJJG arrived at bridge
PJJG crossing bridge
PJJG Exiting bridge
Car(name: BLSQ, time: 7, direction: 1)
Car(name: ZXPH, time: 6, direction: 0)
ZXPH arrived at bridge
BLSQ arrived at bridge
BLSQ crossing bridge
ZXPH crossing bridge <<----!!!!! ERROR.. ZXPH with direction 0 cannot enter the single lane bridge because BLSQ with dir 1 is still crossing the Bridge.
....

Python Elevator Code Simulation Error

I'm really new to this, and am always stuck by basic issues. Please can you show and explain my errors in this chunk of code. Right now the problem is that self is not defined, but I don't understand why.
It would be amazing if someone could clear this up.
import random
import time
class Building:
number_of_floors = 0
customer_list = []
elevator = 0
def __init__(self, floors, customers):
self.number_of_floors = floors
for customerID in range(1, customers + 1):
new = Customer(customerID,self.number_of_floors)
self.customer_list.append(new)
self.customer_list.sort(key = lambda x: x.current_floor)
self.elevator = Elevator(floors,self.customer_list)
self.run()
def run(self):
print('++++++++++++++++++++++++++ELEVATOR IS NOW STARTING+++++++++++++++')
print('Ther are %d customers in the building' % (len(self.customer_list)))
number_of_customers = len(self.customer_list)
self.output()
def output(self):
for customer in self.customer_list:
print("Customer",customer.customerID,"is on floor",customer.current_floor,"and wants to go",customer.destination_floor)
# ELEVATOR MOVING UP
while (self.elevator.current_floor <= self.number_of_floors) & (self.elevator.current_floor > 1):
self.elevator.current_floor -= 1
print(len(self.customer_list),'Customers in lift.')
print('ELEVATOR MOVING DOWN')
print('++++++++++++++++++++++++++++++++++++++++++++++++++++')
print('FLOOR',self.elevator.current_floor)
for customer in self.customer_list:
if (customer.in_elevator == True):
customer.current_floor = self.elevator.current_floor
if (slef.elevator.current_floor == customer.destination_floor) & (customer.in_elevator == True) & (customer.customer_direction == -1):
customer.in_elevator = False
self.customer_list.remove(customer)
print('Customer',customer.customerID,'has reached their destination')
print('There are',len(self.customer_list),'trapped in the elevator')
print('There are',len(Elevator.register_list),'people left on the register')
print('Elevator run is done!!!')
print('CUSTOMERS STUCK IN LIFT ARE BELOW')
for stuck in self.customer_list:
print('Cust. ID:',stuck.customerID,'Dest. Floor:',stuck.destination_floor,'Curr. Floor:',stuck.current_floor,'In Elevator',stuck.in_elevator,'Direction',stuck.customer_direction)
class Elevator:
number_of_floors = 0
register_list = []
current_floor = 0
up = 1
down = -1
def __init__(self, number_of_floors, register_list):
self.number_of_floors = number_of_floors
self.register_list = register_list
def move(self):
pass;
def register_customer(self, customers):
for reg in customers:
self.register_list.append(reg)
def cancel_customer(self, customers):
pass;
class Customer:
current_floor = 0
destination_floor = 0
customerID = 0
in_elevator = False
finished = False
customer_direction = 0
def __init__(self, customerID, floors):
self.customerID = customerID
self.current_floor = random.randint(1, floors)
self.destination_floor = random.randint(1, floors)
while self.destination_floor == self.current_floor:
self.desination_floor = random.randint(1, floors)
if self.current_floor < self.destination_floor:
self.customer_direction = 1
else:
self.customer_direction = -1
def header(): # elevator animation at beginning of program
print(" ELEVATOR OPENING ")
time.sleep(.2)
print("+++++++++++++++++++++++++++++||+++++++++++++++++++++++++++++++++++")
time.sleep(.2)
print("+++++++++++++++++++++++++| |++++++++++++++++++++++++++++++++")
time.sleep(.2)
print("++++++++++++++++| |+++++++++++++++++++")
time.sleep(.2)
print("++++++| |+++++++++")
time.sleep(.2)
print(" ")
time.sleep(.2)
print(" ELEVATOR CLOSING ")
time.sleep(.2)
print("++++++| |+++++++++")
time.sleep(.2)
print("++++++++++++++++| |+++++++++++++++++++")
time.sleep(.2)
print("+++++++++++++++++++++++++| |++++++++++++++++++++++++++++++++")
time.sleep(.2)
print("+++++++++++++++++++++++++++++||+++++++++++++++++++++++++++++++++++")
def main():
try:
floors = int(input('Enter the number of floors: '))
customers = int(input('Enter number of customers: '))
building = Building(floors, customers)
except ValueError:
print('YOU DIDNT ENTER A NUMBER. START AGAIN.')
main()
if __name__ == "__main__":
main()
You haven't indented the functions in your Building class.

Python Formatting Output

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", ""}
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

Loops incorrectly then crashes

I've got a piece of code I'm working on for a school project and the basic idea is that there's a battle between two characters where the user can input 2 attributes to each character: Strength and Skill. Now there are also modifiers for skill and strength which is the difference of the player's skill and strength attributes, divided by 5 and then rounded down respectively. Then each player has a dice roll, depending on who got the higher roll, that player gets the skill and strength modifiers added to their given attributes whilst the one that lost gets the modifiers deducted from their score. This then repeats until the strength attribute of one of them reaches 0 where that player then dies and the game ends.
In my code, the dice rolling function runs twice, and then crashes for an unknown reason I cannot identify.
import random
import time
import math
import sys
strength_mod = 0
skill_mod = 0
def delay_print(s):
for c in s:
sys.stdout.write( '%s' % c )
sys.stdout.flush()
time.sleep(0.05)
def str_mod(charone_str,chartwo_str):
if charone_str > chartwo_str:
top = charone_str
bottom = chartwo_str
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
elif chartwo_str > charone_str:
top = chartwo_str
bottom = charone_str
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
elif charone_str == chartwo_str:
top = charone_str
bottom = chartwo_str
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
def skl_mod(charone_skl, chartwo_skl):
if charone_skl > chartwo_skl:
top = charone_skl
bottom = chartwo_skl;
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
elif chartwo_skl > charone_skl:
top = chartwo_skl
bottom = charone_skl
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
elif charone_skl == chartwo_skl:
top = charone_skl
bottom = chartwo_skl
calc = top - bottom
calc = calc / 5
calc = math.floor(calc)
return calc
def mods():
global strength_mod
global skill_mod
strength_mod = str_mod(charone_strength, chartwo_strength)
skill_mod = skl_mod(charone_skill, chartwo_skill)
print "\nFor this battle, the strength modifier is:",strength_mod
time.sleep(0.20)
print "For this battle, the skill modifier is: ",skill_mod
time.sleep(0.20)
diceroll(charone, chartwo)
print "\n"+str(charone)+"'s","dice roll is:",player1
time.sleep(1)
print "\n"+str(chartwo)+"'s","dice roll is:",player2
def diceroll(charone, chartwo):
print "\n"+str(charone)+" will roll the 6 sided dice first!"
time.sleep(0.5)
global player1
player1 = random.randint(1,6)
delay_print("\nRolling dice!")
global player2
player2 = random.randint(1,6)
time.sleep(0.5)
print "\nNow",chartwo,"will roll the 6 sided dice!"
time.sleep(0.5)
delay_print("\nRolling dice!")
def battle(charone_str, chartwo_str, charone_skl, chartwo_skl, str_mod, skl_mod):
global charone_strength
global charone_skill
global chartwo_strength
global chartwo_skill
if player1 == player2:
print "\nThis round is a draw! No damage done"
elif player1 > player2:
charone_strength = charone_str + str_mod
charone_skill = charone_skl + skl_mod
chartwo_strength = charwo_skl - str_mod
chartwo_skill = chartwo_skl - skl_mod
print "\n"+charone+" won this round"
print "\n"+"Character 1:",charone
print "Strength:",charone_strength
print "Skill:",charone_skill
time.sleep(1)
print "\nCharacter 2:",chartwo
print "Strength:",chartwo_strength
print "Skill:",chartwo_skill
elif player2 > player1:
chartwo_strength = chartwo_str + str_mod
chartwo_skill = chartwo_skl + skl_mod
charone_strength = charone_str - str_mod
charone_skill = charone_skl - skl_mod
print "\n"+chartwo+" won this round"
print "\nCharacter 2:",chartwo
print "Strength:",chartwo_strength
print "Skill:",chartwo_skilll
time.sleep(1)
print "\n"+"Character 1:",charone
print "Strength:",charone_strength
print "Skill:",charone_skill
if charone_skill >= 0:
charone_skill = 0
elif chartwo_skill >= 0:
chartwo_skill = 0
if charone_strength <= 0:
print charone,"has died!",chartwo,"wins!"
elif chartwo_strength <= 0:
print chartwo,"has died!",charone,"wins!"
charone = raw_input("Enter the name of character one: ")
chartwo = raw_input("Enter the name of character two: ")
time.sleep(1.5)
print "\n",charone,"encounters",chartwo
delay_print("\nBattle Initiated!")
charone_strength = int(raw_input("\nEnter the strength score for "+str(charone)+" (between 50 and 100): "))
while charone_strength > 100 or charone_strength < 50:
print "That number is not between 50-100"
charone_strength = int(raw_input("\nEnter the strength score for "+str(charone)+" (between 50 and 100): "))
else:
pass
charone_skill = int(raw_input("Enter the skill score for "+str(charone)+" (between 50 and 100): "))
while charone_skill > 100 or charone_skill < 50:
print "That number is not between 50-100"
charone_skill = int(raw_input("Enter the skill score for "+str(charone)+" (between 50 and 100): "))
else:
pass
time.sleep(1.0)
chartwo_strength = int(raw_input("\nEnter the strength score for "+str(chartwo)+" (between 50 and 100): "))
while chartwo_strength > 100 or chartwo_strength < 50:
print "That number is not between 50-100"
chartwo_strength = int(raw_input("\n Enter the strength score for "+str(chartwo)+" (between 50 and 100): "))
else:
pass
chartwo_skill = int(raw_input("Enter the skill score for "+str(chartwo)+" (between 50 and 100): "))
while chartwo_skill > 100 or chartwo_skill < 50:
print "That number is not between 50-100"
chartwo_skill = int(raw_input("Enter the skill score for "+str(chartwo)+" (between 50 and 100): "))
else:
pass
time.sleep(2)
print "\nCharacter 1:",charone
print "Strength:",charone_strength
print "Skill:",charone_skill
time.sleep(1)
print "\nCharacter 2:",chartwo
print "Strength:",chartwo_strength
print "Skill:",chartwo_skill
time.sleep(1)
while charone_strength != 0 or chartwo_strength != 0:
ent = raw_input("Press Enter to roll! ")
mods()
diceroll(charone,chartwo)
battle(charone_strength, chartwo_strength, charone_skill, chartwo_skill, str_mod, skl_mod)
else:
play = raw_input("\nWould you like to play again?")
if play in ["yes","y","Yes","Y"]:
execfile("gcse.py")
else:
print "Goodbye"
This code is an excellent example of how not to program:
heavy use of global variables
multiple variables with very similar names (ie charone_strength, charone_str)
reuse of names in different contexts (in global scope, str_mod is a function, but in function battle it is supposed to be an integer
Your immediate problem: in line 182, you call
battle(charone_strength, chartwo_strength, charone_skill, chartwo_skill, str_mod, skl_mod)
which should be
battle(charone_strength, chartwo_strength, charone_skill, chartwo_skill, strength_mod, skill_mod)
but is confused because in the function strength_mod is referred to as str_mod.
Here is a greatly cleaned-up version. (Further suggestions for improvement are welcome).
from __future__ import division, print_function
from random import randint
import sys
from time import sleep
# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
rng = xrange
else:
# Python 3.x
inp = input
rng = range
PRINT_DELAY = 0.02
PAUSE_DELAY = 0.2
TF_VALUES = {
'y': True, 'yes': True, 't': True, '': True,
'n': False, 'no': False, 'f': False
}
def get_int(prompt="Enter an integer: ", lo=None, hi=None):
while True:
try:
i = int(inp(prompt))
if (lo is None or lo <= i) and (hi is None or i <= hi):
return i
except ValueError:
pass
def get_tf(prompt="Yes or no? ", tf_values=TF_VALUES):
while True:
s = inp(prompt).strip().lower()
tf = tf_values.get(s, None)
if tf is not None:
return tf
def pause(delay=PAUSE_DELAY):
sleep(delay)
def roll(die_sides=6):
return randint(1, die_sides)
def slow_print(s, delay=PRINT_DELAY):
for ch in s:
print(ch, end='', flush=True)
sleep(delay)
print('') # end of line
def wait_for_enter(prompt):
inp(prompt)
class Fighter:
#classmethod
def get_fighter(cls, prompt):
print(prompt)
name = inp ("Name: ")
health = get_int("Health: (50-100) ", 50, 100)
skill = get_int("Skill: (50-100) ", 50, 100)
return cls(name, health, skill)
def __init__(self, name, health, skill):
self.name = name
self.health = health
self.skill = skill
def is_alive(self):
return self.health > 0
def health_mod(self, other_fighter):
delta = abs(self.health - other_fighter.health)
return int(delta / 5)
def skill_mod(self, other_fighter):
delta = abs(self.skill - other_fighter.skill)
return int(delta / 5)
def attack(self, other_fighter):
wait_for_enter("Hit Enter to fight!")
# figure out mod values
health_mod = self.health_mod(other_fighter)
skill_mod = self.skill_mod (other_fighter)
self_roll = roll()
other_roll = roll()
slow_print(
"Health mod: {} Skill mod: {} {} rolls {} {} rolls {}"
.format(
health_mod, skill_mod,
self.name, self_roll,
other_fighter.name, other_roll
)
)
# figure out who won this round
if self_roll == other_roll:
print("Draw! No damage done.")
else:
winner, loser = (self, other_fighter) if self_roll > other_roll else (other_fighter, self)
print("{} hits {}!".format(winner.name, loser.name))
winner.health += health_mod
winner.skill += skill_mod
loser.health -= health_mod
loser.skill -= skill_mod
# show results
print('')
print(self)
print(other_fighter)
print('')
def __str__(self):
return "{}: health {}, skill {}".format(self.name, max(self.health, 0), max(self.skill, 0))
def fight():
f1 = Fighter.get_fighter("\nFirst fighter:")
pause()
f2 = Fighter.get_fighter("\nSecond fighter:")
pause()
slow_print("\n{} encounters {}\nBattle Initiated!".format(f1.name, f2.name))
while f1.is_alive() and f2.is_alive():
f1.attack(f2)
winner, loser = (f1, f2) if f1.is_alive() else (f2, f1)
print("{} has died; {} wins!".format(loser.name, winner.name))
def main():
while True:
fight()
if not get_tf("Would you like to play again? (Y/n)"):
print("Goodbye!")
break
if __name__=="__main__":
main()

A little misunderstanding timers in python

can somebody tell me how to use this class timers from python in my code more than one time.
import MOD
class timer:
def __init__(self, seconds):
self.start(seconds)
def start(self, seconds):
self.startTime = MOD.secCounter()
self.expirationTime = self.startTime + seconds
if seconds != 0:
self.running = 1
self.expired = 0
else:
self.running = 0
self.expired = 0
def stop(self):
self.running = 0
self.expired = 0
def isexpired(self):
if self.running == 1:
timeNow = MOD.secCounter()
if timeNow > self.expirationTime:
self.running = 0
self.expired = 1
else:
self.expired = 0
return self.expired
def isrunning(self):
if self.running == 1:
timeNow = MOD.secCounter()
if timeNow > self.expirationTime:
self.running = 0
self.expired = 1
else:
self.expired = 0
return self.running
def change(self, seconds):
self.expirationTime = self.startTime + seconds
def count(self):
if self.running == 1:
timeNow = MOD.secCounter()
return (timeNow - self.startTime)
else:
return -1
they write this comment:
The following is a simple example about how to use this class:
import timers
timerA = timers.timer(0)
timerA.start(15)
while 1:
if timerA.isexpired():
print 'timerA expired'
break
but I don't know how to use it more than one time in my code, because I need to use more than one timer in my code,
should I write
timerB = timers.timer(1)
timerB.start(1800)
while 1:
if timerB.isexpired():
print 'timerA expired'
break
any help, please
Close - the argument to timers.timer is the number of seconds that the timer should time for at first. But every time you call timers.timer(), you'll get a new timer instance.
So your code could look more like:
timerB = timers.timer(1800)
while 1:
if timerB.isexpired():
print 'timerA expired'
break
Except that this is misleading - timerA and timerB are separate timers, so timerB.isexpired() won't tell you anything about timerA. Maybe you meant it to read "timerB expired"?
And I would also advise against polling timerB.isexpired() so rapidly. Maybe sleep for a second after each check?

Categories

Resources