i am new to python programming. This is code that i want to improve
# Critter Caretaker
# A virtual pet to care for
class Critter(object):
"""A virtual pet"""
def __init__(self, name, hunger = 0, boredom = 0):
self.name = name
self.hunger = hunger
self.boredom = boredom
def __pass_time(self):
self.hunger += 1
self.boredom += 1
#property
def mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
m = "happy"
elif 5 <= unhappiness <= 10:
m = "okay"
elif 11 <= unhappiness <= 15:
m = "frustrated"
else:
m = "mad"
return m
def talk(self):
print("I'm", self.name, "and I feel", self.mood, "now.\n")
self.__pass_time()
def eat(self, food = 4):
print("Brruppp. Thank you.")
self.hunger -= food
if self.hunger < 0:
self.hunger = 0
self.__pass_time()
def play(self, fun = 4):
print("Wheee!")
self.boredom -= fun
if self.boredom < 0:
self.boredom = 0
self.__pass_time()
def __str__(self):
rep = "Attribut value is\n"
rep += str(self.hunger) + "\n" + str(self.boredom) + "\n"
return rep
def main():
crit_name = input("What do you want to name your critter?: ")
crit = Critter(crit_name)
choice = None
while choice != "0":
print \
("""
Critter Caretaker
0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter
""")
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# listen to your critter
elif choice == "1":
crit.talk()
# feed your critter
elif choice == "2":
crit.eat()
# play with your critter
elif choice == "3":
crit.play()
# secret option
elif choice == "!":
print(crit)
# some unknown choice
else:
print("\nSorry, but", choice, "isn't a valid choice.")
main()
("\n\nPress the enter key to exit.")
You see it is a puppet that show his mood and search of you to feed and play with him. Every time puppet talk, eat or play counter pass_time add 2 level up. Attribute food and fun have always the same number that directly affects mood. I want to make that user can enter amount of food and time that can affect mood of puppet. Why i cant do this
def eat(self, food):
print("Brruppp. Thank you.")
self.hunger -= food
if self.hunger < 0:
self.hunger = 0
self.__pass_time()
def play(self, fun):
print("Wheee!")
self.boredom -= fun
if self.boredom < 0:
self.boredom = 0
self.__pass_time()
and this
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# listen to your critter
elif choice == "1":
crit.talk()
# feed your critter
elif choice == "2":
food = int(input("How much food?: "))
crit.eat()
# play with your critter
elif choice == "3":
fun = int(input("How much time?: "))
crit.play()
# secret option
elif choice == "!":
print(crit)
# some unknown choice
else:
print("\nSorry, but", choice, "isn't a valid choice.")
You seem to be almost there if I understand your question right. Lets use the eat function as an example. You want to pass the food variable to the class function to use. If you want you can also add validation to check if that value entered is a number.
elif choice == "2":
food = int(input("How much food?: "))
crit.eat(food) # pass food in as a parameter
Related
I don't understand why I can't connect the processSale method to the sellProduct method. I think that the classes and other methods don't need any change because I only followed the criterias that were given to me.
#candy machine
class CashRegister:
def __init__(self, cashOnHand = 500,):
if cashOnHand < 0:
self.cashOnHand = 500
else:
self.cashOnHand = cashOnHand
def currentBalance(self):
return self.cashOnHand
def acceptAmount(self, cashIn):
self.cashOnHand += cashIn
class Dispenser:
def __init__(self, numberOfItems = 50, productCost = 50):
if numberOfItems < 0:
self.numberOfItems = 50
else:
self.numberOfItems = numberOfItems
if productCost < 0:
self.productCost = 50
else:
self.productCost = productCost
def getCount(self):
return self.numberOfItems
def getProductCost(self):
return self.productCost
def makeSale(self):
self.numberOfItems -= 1
class MainProgram:
def showMenu(self):
global userInput
print("**** Welcome to Eros' Candy Shop ****")
print("To select an item enter")
print("""1 for Candy
2 for Chips
3 for Gum
4 for Cookies
0 to View Balance
9 to Exit""")
userInput = int(input("Enter your choice: "))
def sellProduct(self, useDispenser = Dispenser(), useRegister = CashRegister()):
try:
self.useDispenser = useDispenser
self.useRegister = useRegister
if self.useDispenser.getCount != 0:
print(f"It costs {self.useDispenser.getProductCost} cents")
cash = int(input("Please enter your payment: "))
change = cash - self.useDispenser.getProductCost
if change < 0:
print("Insufficient money!")
print(f"You need {self.useDispenser.getProductCost - cash} cents more")
return
else:
print(f"Your change is {change} cents")
self.useRegister.acceptAmount(self.useDispenser.getProductCost)
self.useDispenser.makeSale
return
elif self.useDispenser.getCount == 0:
print("The product you chose is sold out! Try the other itmes")
return
except ValueError:
print("You entered an incorrect value. Please use the numbers on the menu only")
def processSale(self):
Register = CashRegister()
Candy = Dispenser()
Chips = Dispenser()
Gum = Dispenser()
Cookies = Dispenser()
while True:
self.showMenu
if userInput == 1:
self.sellProduct(Candy, Register)
elif userInput == 2:
self.sellProduct(Chips, Register)
elif userInput == 3:
self.sellProduct(Gum, Register)
elif userInput == 4:
self.sellProduct(Cookies, Register)
elif userInput == 0:
print("Current Balance is" + str(Register.currentBalance))
elif userInput == 9:
break
mainProgram = MainProgram()
mainProgram.showMenu()
How do i use sellProduct method on userInput 1-4. I get confused when applying the properties of a class and how to connect them. Can you point out what mistakes I made and what other improvements I can do.
here are some points you can improve :
When you call your methods do not forget the parenthesis :
self.useDispenser.getCount()
self.useDispenser.getProductCost()
Create an infinite loop to continuously ask for input within showMenu and delete the one within processSale (for example):
def showMenu(self):
global userInput
userInput = 0
print("**** Welcome to Eros' Candy Shop ****")
while userInput != 9:
print("To select an item enter")
print(MENU)
userInput = int(input("Enter your choice: "))
if userInput < 9:
self.processSale()
But please update the whole program accordingly.
Hope it helps !
I am learning about classes in Python and tried to make a simple battle game. This code runs fine and as long as I print devin.battle() multiple times, but I want a while loop so that it will revert back to asking if the user would like to start a battle while also deducting the attack from the hit points.
start_battle = input("Would you like to start a battle? Y/N ===> ")
class People:
max_hit_points = 150
current_hit_points = 150
current_strength = 5
defence = 0
def __init__(self, name, current_hit_points):
self.name = name
self.current_hit_points
self.damage = 50
def battle(self):
if start_battle == "Y":
self.attacked()
elif start_battle == "N":
print("okay, nevermind then")
def attacked(self):
self.current_hit_points -= self.damage
print("You have been attacked")
if self.current_hit_points > 0:
print("Try again newb!")
else:
print("Your HP has reached 0, you are dead")
def __str__(self):
return f"{self. name} has {self.current_hit_points}HP remaining"
devin = People("Devin", 150)
devin.battle()
print(devin)
However, when I add one to the whole thing Python freezes up.
start_battle = input("Would you like to start a battle? Y/N ===> ")
class People:
max_hit_points = 150
current_hit_points = 150
current_strength = 5
defence = 0
def __init__(self, name, current_hit_points):
self.name = name
self.current_hit_points
self.damage = 50
while current_hit_points > 0:
def battle(self):
if start_battle == "Y":
self.attacked()
elif start_battle == "N":
print("okay, nevermind then")
def attacked(self):
self.current_hit_points -= self.damage
print("You have been attacked")
if self.current_hit_points > 0:
print("Try again newb!")
else:
print("Your HP has reached 0, you are dead")
def __str__(self):
return f"{self. name} has {self.current_hit_points}HP remaining"
continue
else:
print("You are dead")
devin = People("Devin", 150)
devin.battle()
print(devin)
If anyone can explain to me why its freezing up and how I can properly loop a battle, it would be greatly appreciated.
You have multiple issues here and its not super clear what you are trying to do but here is some direction according to your comment, make sure ot read the # code comments
class People:
max_hit_points = 150
current_hit_points = 150
current_strength = 5
defence = 0
def __init__(self, name, current_hit_points):
self.name = name
# you forgot to actually assign the hit points
self.current_hit_points = current_hit_points
self.damage = 50
def battle(self):
while self.current_hit_points > 0:
# you want to ask the user every round?
start_battle = input("Would you like to start a battle? Y/N ===> ")
if start_battle == "Y":
self.attacked()
# note that the user didnt get to attack back
# maybe print(self) here so the user can see his hp?
elif start_battle == "N":
print("okay, nevermind then")
break
else:
print("You are dead")
def attacked(self):
self.current_hit_points -= self.damage
print("You have been attacked")
if self.current_hit_points > 0:
print("Try again newb!")
else:
print("Your HP has reached 0, you are dead")
def __str__(self):
return f"{self. name} has {self.current_hit_points}HP remaining"
devin = People("Devin", 150)
devin.battle()
print(devin)
I want to make an RPG game, and I'm trying to make a system of buying items and potions. What I intended was for the player to get 3 potions of each in the beginning, but you need to buy more to continue with them. My problem is that they keep on resetting every time I call the fight function.
I've tried making them global in the beginning and defining them, but they keep on saying "Referenced before assignment"
def Fight(monster):
global HealPotionsLeft
global WeakPotionsLeft
HealPotionsLeft = 3
WeakPotionsLeft = 3
Potion = ['Yes', 'No']
currentFighter = ""
if myPlayer.dexterity >= monster.dexterity:
currentFighter = myPlayer.name
else:
currentFighter = monster.name
while myPlayer.isDead is not True and monster.isDead is not True:
print(currentFighter + "'s turn!")
print("===========================")
print("Name:", myPlayer.name)
print("Health:", myPlayer.health, "/", myPlayer.maxHealth)
print("===========================")
print("Name:", monster.name)
print("Health:", monster.health, "/", monster.maxHealth)
print("===========================")
if currentFighter == monster.name:
monster.Attack(myPlayer)
currentFighter = myPlayer.name
continue
userInput = ""
validInput = False
while validInput is not True:
print("-Attack")
print("-Spells")
print("-Items")
print("-Flee")
userInput = input()
if userInput == "Attack":
myPlayer.Attack(monster)
break
if userInput == "Spells":
print("TO DO - Spells")
if userInput == "Items":
secure_random = random.SystemRandom()
item = secure_random.choice(Potion)
if item == ('Yes'):
print("You have", HealPotionsLeft, "Potions of Healing Left and", WeakPotionsLeft, "Potions of Damage Left.")
PotionUsage = input("Would you like to use your *Potion of Healing*? y/n")
if PotionUsage == str("n"):
if HealPotionsLeft == 0:
print("You spent too much time trying to get the healing potion so you got attacked! *Out of Healing Potions*.")
break
elif HealPotionsLeft > 0:
if PotionUsage == ("y"):
myPlayer.health = 100
print(myPlayer.name, "Healed to 100 HP!")
HealPotionsLeft = HealPotionsLeft - 1
PotionsLeft()
break
if PotionUsage == str("y"):
if WeakPotionsLeft == 0:
print("You spent too much time trying to get the Potion of Damage so you got attacked! *Out of Potions of Damage*.")
break
elif WeakPotionsLeft > 0:
weakPotion = input("Would you like to use your Potion of Damage? y/n")
if weakPotion == str("y"):
monster.health = monster.health - 20
print(myPlayer.name, "Used their Potion of Damage on" , monster.name, "for 20 damage!")
WeakPotionsLeft = WeakPotionsLeft - 1
PotionsLeft()
break
if item == ('No'):
print("You didn't get to your potions in time!")
break
I expect the potions to go to three when the player goes into battle in the first time, but afterwards when going to battle the amount of potions resets the the amount remaining from last battle.
Outside this Fight() function initialize your potion counts to 3 each. Then pass the current amount of potions in to the Fight() function something like: Fight(monster,Hpots,Wpots) then return the remaining potions to the outer scope with a return(HealPotionsLeft,WeakPotionsLeft)
********* Example Requested: *********
I can not test this code and this is just an example
BattleResults = []
global CurrentHealPotions
global CurrentWeakPotions
CurrentHealPotions = 3
CurrentWeakPotions = 3
def Fight(monster,HealPotionsLeft,WeakPotionsLeft):
Potion = ['Yes', 'No']
currentFighter = ""
if myPlayer.dexterity >= monster.dexterity:
currentFighter = myPlayer.name
else:
currentFighter = monster.name
while myPlayer.isDead is not True and monster.isDead is not True:
print(currentFighter + "'s turn!")
print("===========================")
print("Name:", myPlayer.name)
print("Health:", myPlayer.health, "/", myPlayer.maxHealth)
print("===========================")
print("Name:", monster.name)
print("Health:", monster.health, "/", monster.maxHealth)
print("===========================")
if currentFighter == monster.name:
monster.Attack(myPlayer)
currentFighter = myPlayer.name
continue
userInput = ""
validInput = False
while validInput is not True:
print("-Attack")
print("-Spells")
print("-Items")
print("-Flee")
userInput = input()
if userInput == "Attack":
myPlayer.Attack(monster)
break
if userInput == "Spells":
print("TO DO - Spells")
if userInput == "Items":
secure_random = random.SystemRandom()
item = secure_random.choice(Potion)
if item == ('Yes'):
print("You have", HealPotionsLeft, "Potions of Healing Left and", WeakPotionsLeft, "Potions of Damage Left.")
PotionUsage = input("Would you like to use your *Potion of Healing*? y/n")
if PotionUsage == str("n"):
if HealPotionsLeft == 0:
print("You spent too much time trying to get the healing potion so you got attacked! *Out of Healing Potions*.")
break
elif HealPotionsLeft > 0:
if PotionUsage == ("y"):
myPlayer.health = 100
print(myPlayer.name, "Healed to 100 HP!")
HealPotionsLeft = HealPotionsLeft - 1
PotionsLeft()
break
if PotionUsage == str("y"):
if WeakPotionsLeft == 0:
print("You spent too much time trying to get the Potion of Damage so you got attacked! *Out of Potions of Damage*.")
break
elif WeakPotionsLeft > 0:
weakPotion = input("Would you like to use your Potion of Damage? y/n")
if weakPotion == str("y"):
monster.health = monster.health - 20
print(myPlayer.name, "Used their Potion of Damage on" , monster.name, "for 20 damage!")
WeakPotionsLeft = WeakPotionsLeft - 1
PotionsLeft()
break
if item == ('No'):
print("You didn't get to your potions in time!")
break
if myPlayer.isDead is True
result="You have been defeated!"
else
result="You have slain the Beast!"
BattleEnd=[result, HealPotionsLeft, WeakPotionsLeft]
return(BattleEnd)
A call to this function might look like:
BattleResults = Fight("Your Monster Reference Here",CurrentHealPotions,CurrentWeakPotions)
Then assign the new values to potions:
CurrentHealPotions = BattleResults[1]
CurrentWeakPotions = BattleResults[2]
I'm using a book called Python for Absolute Beginners and am doing a challenge in the book's chapter 8 (Software Objects). My code works but I want to add another capability to the code; What code do I need to add to feed/play/talk with a specific critter?
In choice #7 of my code (7 - Feed a specific critter), I have written some code but it doesn't work. How might I access/change the attributes of a specific object that the user selects?
Also, any feedback to improve/shorten the code?
# Critter Caretaker
# A virtual pet to care for
class Critter(object):
import random
"""A virtual pet"""
def __init__(self, name, hunger = random.randint(0,9), boredom = random.randint(0,11)):
self.name = name
self.hunger = hunger
self.boredom = boredom
def __pass_time(self):
self.hunger +=1
self.boredom +=1
def __str__(self):
m = "name is: {}, hunger is: {}, boredom is: {}".format(self.name, self.hunger,self.boredom)
return m
#property
def mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness <5:
m = "happy"
elif 5 <= unhappiness <=10:
m = "okay"
elif 11 <= unhappiness <=15:
m = "frustrated"
else:
m= "mad"
return m
def talk(self):
print("I'm", self.name, "and I feel", self.mood, "now.\n")
self.__pass_time()
def eat(self, food=3):
print("Yummy! Thank you.")
self.hunger -=food
if self.hunger <0:
self.hunger = 0
self.__pass_time()
def play(self, fun=4):
print("That was fun! Thanks")
self.boredom -=fun
if self.boredom <0:
self.boredom = 0
self.__pass_time()
def main():
noc = []
noc.append(Critter("Chicken"))
print("An critter named Chicken has been created")
choice = None
while choice != "0":
print \
("""
Critter Caretaker
0 - Quit
1 - Listen to all your critters
2 - Feed all your critters
3 - Play with all your critters
4 - Create a new critter
5 - Show a list of critters
6 - Delete a critter
7 - Feed a specific critter
""")
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# listen to critters
elif choice == "1":
if len(noc) != 0:
i.talk()
else:
print("No critters exist")
# feed your critters
elif choice == "2":
if len(noc) != 0:
for i in noc:
print(i.name, "says:", end = " ")
i.eat()
# play with your critters
elif choice == "3":
if len(noc) != 0:
for i in noc:
print(i.name, "says:", end = " ")
i.play()
else:
print("No critter exists")
# Create a new critter
elif choice == "4":
noc.append(Critter(input("Enter a name for your new critter: ")))
# Show critters
elif choice =="5":
b = 0
if len(noc) != 0:
for i in noc:
b +=1
print(b, "." ,i.name)
else:
print("No critter exists")
# delete a critter
elif choice == "6":
a = int(input("Enter the serial number of critter you want to delete: "))
a = a -1
del noc[a]
# feed a specific critter
elif choice == "7":
sctd = input("Enter name of critter you want to feed: ")
if sctd in noc:
noc.eat()
else:
print("That critter doesn't exists")
# print attributes of critters
elif choice == "919":
for i in noc:
print(i)
else:
print("\nSorry, but", choice, "isn't valid.")
main()
("\n\nPress the enter key to exit.")
In the branch for feeding a specific critter, you have the line noc.eat() -- noc is a list, which has no eat() method. You need to access the critter specified by the input, and call the eat() method on that object. Something like:
for c in noc:
if c.name == sctd:
c.eat()
break
(although there is an issue with this example if multiple critters match sctd -- solving that is something i'll leave to you)
an additional minor point of feedback on your code: a docstring should be the first statement inside the definition as per PEP 0257.
Just a small note is that I think the convention something like import random should probably be placed at the start rather than inside the class which also gets rid of the problem raised by #nthall which is that your docstring is currently not the first object.
I attempted to recreate a piece of code which is basically a minimalist Tomagatchi like thing. However when it is fed, and listened to, it's "mood" value does not change. It remains "mad". Any help would be greatly appreciated!
{#Create name, hunger, boredom attributes. Hunger and Boredom are numberical attributes
class Critter(object):
def __init__(self, name, hunger = 0, boredom = 0):
self.name = name
self.hunger = hunger
self.boredom = boredom
#Creating a private attribute that can only be accessed by other methods
def __pass_time(self):
self.hunger += 1
self.boredom += 1
def __get_mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
mood = "happy"
if 5 <= unhappiness <= 10:
mood = "okay"
if 11 <= unhappiness <= 15:
mood = "frustrated"
else:
mood = "mad"
return mood
mood = property (__get_mood)
def talk(self):
print "Hi! I'm",self.name,"and I feel",self.mood,"now."
self.__pass_time()
def eat(self, food = 4):
print "Brrruuup. Thank you!"
self.hunger -= food
if self.hunger < 0:
self.hunger = 0
self.__pass_time()
def play(self, play = 4):
print "Yaaay!"
self.boredom -= play
if self.boredom < 0:
self.boredom = 0
self.__pass_time()
def main ():
crit_name = raw_input("What do you want to name your critter? ")
crit = Critter (crit_name)
choice = None
while choice != "0":
print \
""" 0 - Quit
1 - Listen to your critter.
2 - Feed your critter
3 - Play with your critter
"""
choice = raw_input ("Enter a number: ")
#exit
if choice == "0":
print "GTFO."
#listen to the critter
elif choice == "1":
crit.talk()
#feed the crit crit critter
elif choice == "2":
crit.eat()
#play with the crit crit critter
elif choice == "3":
crit.play()
#some unknown choice
else:
print "\nwat"
main ()
raw_input ("\n\nHit enter to GTFO")
In _getMood, there should be elifs.
if unhappiness < 5:
mood = "happy"
elif 5 <= unhappiness <= 10:
mood = "okay"
elif 11 <= unhappiness <= 15:
mood = "frustrated"
else:
mood = "mad"
Without them, it was actually only checking if unhappiness was between 11 and 15, and if not, setting the mood to mad. So unhappines from 0 to 10, and up from 16 meaned a critter was mad.
I would say in cases like this, subsitute for a variable and trace the code.
unhappiness = 3
if unhappiness < 5:
mood = "happy"
ok we became happy
if 5 <= unhappiness <= 10:
mood = "okay"
nothing happened as 3 is not in range for 5 <= x <= 10
if 11 <= unhappiness <= 15:
mood = "frustrated"
nothing happened as 3 is not in range for 11 < x < 15
else:
mood = "mad"
else what? ah this refers to the last condition. so if it's not 11 < x < 15 then we're mad.
Subtitution of a variable with a value, and then tracing the code, line by line, is generally what you should try in situations like this, at least until it becomes second nature.