Looping and ignoring flag - Python - python

I am working on learning python and decided to write a small battle engine to use a few of the different items I have learned to make something a bit more complicated. The problem I am having is that I set a selection that the user makes that should cause one of either two parts to load, but instead it skips to my loop instead of performing the selection. Here is what I have so far:
import time
import random
import sys
player_health = 100
enemy_health = random.randint(50, 110)
def monster_damage():
mon_dmg = random.randint(5,25)
enemy_health - mon_dmg
print ('You hit the beast for ' + str(mon_dmg) + ' damage! Which brings its health to ' + str(enemy_health))
player_dmg()
def player_dmg():
pla_dmg = random.randint(5,15)
player_health - pla_dmg
print ('The beast strikes out for ' + str(pla_dmg) + ' damage to you. This leaves you with ' + str(player_health))
def run_away():
run_chance = random.randint(1,10)
if run_chance > 5:
print ('You escape the beast!')
time.sleep(10)
sys.exit
else:
print ('You try to run and fail!')
player_dmg()
def player_turn():
print ('Your Turn:')
print ('Your Health: ' + str(player_health) + ' Monsters Health: ' + str(enemy_health))
print ('What is your next action?')
print ('Please Select 1 to attack or 2 to run.')
action = input()
if action == 1:
monster_damage()
elif action == 2:
run_away()
while player_health > 0 and enemy_health > 0:
player_turn()
if player_health <= 0:
print ('The beast has vanquished you!')
time.sleep(10)
sys.exit
elif enemy_health <= 0:
print ('You have vanquished the beast and saved our Chimichongas')
time.sleep(10)
sys.exit

The function input returns a str not an int
action = input()
So this comparison will always return False
if action == 1:
For example
>>> '1' == 1
False
You can convert their input to an int as follows
action = int(input())

Related

Can someone please explain to me why the 'health' var is not updated when calling the updateGame() func

Been looking online for some answers, however it's still unclear to me why the 'health' var is not updated when calling the getDamage() func
I'm on my first few miles learning python
health = 200.0
maxHealth = 200
healthDashes = 20
dashConvert = int(maxHealth/healthDashes)
currentDashes = int(health/dashConvert)
remainingHealth = healthDashes - currentDashes
healthDisplay = '-' * currentDashes
remainingDisplay = ' ' * remainingHealth
percent = str(int((health/maxHealth)*100)) + "%"
gameOver = False
def updateGame():
print(chr(27) + "[2J")
print (30 * '-')
print("")
print(" |" + healthDisplay + remainingDisplay + "|")
print(" health " + percent)
print ("")
print (30 * '-')
print("")
def getDamage():
global health
health = 10
while gameOver == False:
answer = raw_input("> ").lower()
if answer == "help":
print("")
print(" you can use the following commands: h, i, q, d")
print("")
elif answer == "q":
print("\n")
print("Game Over")
print("")
break
elif answer == "h":
updateGame()
elif answer == "d":
getDamage()
else:
print(""" not a valid command, see "help" """)
Is there anything I can do to properly update the "health" var and disply a reduced health the next time I call the getDamage() func?
Basically what I'm trying to achieve is a text-based game to run in a while loop and have different functions to update a primary function (updateGame) that display relevant info about the player's state like health, inventory items.
The logic I'm trying to implement is:
have getDamage() reduce the health var and then display the newly change variable with updateGame()
Many thanks
health will change to 10 when you call getDamage() but healthDisplay, remainingDisplay and percent are set at the first of script and won't change everytime that healt global variable is changed. so you must change them in updateGame() function everytime it's called. Also i guess health = 10 must change to health -= 10!
health = 200.0
maxHealth = 200
healthDashes = 20
gameOver = False
def updateGame():
dashConvert = int(maxHealth/healthDashes)
currentDashes = int(health/dashConvert)
remainingHealth = healthDashes - currentDashes
healthDisplay = '-' * currentDashes
remainingDisplay = ' ' * remainingHealth
percent = str(int((health/maxHealth)*100)) + "%"
print(chr(27) + "[2J")
print (30 * '-')
print("")
print(" |" + healthDisplay + remainingDisplay + "|")
print(" health " + percent)
print ("")
print (30 * '-')
print("")
def getDamage():
global health
health -= 10
while gameOver == False:
answer = input("> ").lower()
if answer == "help":
print("")
print(" you can use the following commands: h, i, q, d")
print("")
elif answer == "q":
print("\n")
print("Game Over")
print("")
break
elif answer == "h":
updateGame()
elif answer == "d":
getDamage()
else:
print(""" not a valid command, see "help" """)
Inside the updateGame function, you never make reference to the global variable health. If you want health to change inside the function, you will need to access it.
This means you should have something like:
def updateGame():
global health
health = updatedHEALTH
...
Then it should change each time you call the function

Not allowing me to use a > between a function and int

GUESSING GAME
from random import randint
from time import sleep
def get_user_guess():
guess = int(input('What is your guess: '))
return guess
def roll_dice(number_of_sides):
first_roll = randint(1, number_of_sides)
second_roll = randint(1, number_of_sides)
max_val = number_of_sides * 2
print ("The maximum value you can roll is %d" % max_val)
get_user_guess()
if get_user_guess > 13:
print ("invalid guess, please try again")
else:
print ("Rolling...")
sleep(2)
print ("%d" % first_roll)
sleep(1)
print ("%d" % second_roll)
sleep(1)
total_roll = first_roll + second_roll
print ("%d" % total_roll)
print ("Result...")
sleep(1)
if get_user_guess == total_roll:
print ("Congratulations, you've won!")
else:
print ("sorry sucker, you lose!")
roll_dice(6)
Here is the code. I made a running version in python 2, but translating it to python 3 has been a headache. I have defined that get_user_guess, where guess = an int. But further down in the roll_dice section, after I have called on the previous function and its answer I'm getting error messages.
That's not how you access the return value of a function. You should assign the return value to a new name, then use that in the following comparisons.
guess = get_user_guess()
if guess > 13:
print("invalid guess")
else:
...
if guess == total_roll:
print("Congratulations")
else:
print("sorry")
So, this seems like a small logic error and a syntax error as well. First the syntax error. You're not really initializing or calling the function in the comparison by doing:
if get_user_guess > 12
rather than doing:
if get_user_guess() > 12
So there is nothing for the ">" operator to compare against.
Second, seeing as you're trying to reuse the variable for the next comparison.You will need to store it as well otherwise it would prompt the user again for a new value again. Note the changes in lines 13,14 and 28 will fix it:
from random import randint
from time import sleep
def get_user_guess():
guess = int(input('What is your guess: '))
return guess
def roll_dice(number_of_sides):
first_roll = randint(1, number_of_sides)
second_roll = randint(1, number_of_sides)
max_val = number_of_sides * 2
print ("The maximum value you can roll is %d" % max_val)
guess = get_user_guess()
if guess > 13:
print ("invalid guess, please try again")
else:
print ("Rolling...")
sleep(2)
print ("%d" % first_roll)
sleep(1)
print ("%d" % second_roll)
sleep(1)
total_roll = first_roll + second_roll
print ("%d" % total_roll)
print ("Result...")
sleep(1)
if get_user_guess == total_roll:
print ("Congratulations, you've won!")
else:
print ("sorry sucker, you lose!")
roll_dice(6)

Blackjack python dice game fix?

When someone says something else in replay() except for "no" or "yes" it starts the host's turn for some reason or even no.
I'm still learning, this is my second project, any comments?
I tried many things but it still doesn't work.
# Blackjack game
import random
global playername, nameofplayer
nameofplayer = input('Enter your name').capitalize()
print ('Hello,', str(nameofplayer))
class Player():
def __init__(self, banktotal):
self.banktotal = banktotal
# adds an amount to the bank
def addtobank(self, bankadd):
self.banktotal += bankadd
# removes an amount from the bank
def subfrombank(self, subbank):
self.banktotal -= subbank
def rolldice():
global playerhand
playerhand = 0
print('Your Current hand: ' + str(playerhand))
playerhand = 0
print ('...')
print ('Rolling the dice for you')
dice = list(range(1,7))
while playerhand <= 21:
rolled = random.choice(dice)
print('Your Current hand: ' + str(playerhand))
hitstick = str(input('Hit or Stick?').capitalize())
if hitstick == 'Hit':
print('You chose to hit!')
playerhand += rolled
elif hitstick == 'Stick':
print('Your exit hand: ' + str(playerhand))
break
elif hitstick != 'Hit' or 'Stick':
print('Enter a valid argument')
else:
print('Your Current hand: ' + str(playerhand))
print ('Busted, Host Wins!')
print('Reducing 100$ from your account')
playername.banktotal -= 100
print('Your bank balance: ' + str(playername.banktotal))
replay()
def hostchance():
hosthand = 0
print ('Current host hand: ' + str(hosthand))
dice = list(range(1, 7))
while hosthand <= playerhand:
rolled = random.choice(dice)
hosthand += rolled
print('Rolling the dice for the host')
print ('Current host hand: ' + str(hosthand))
if hosthand < playerhand:
pass
elif hosthand == playerhand:
print ('Its a draw!')
break
elif hosthand > playerhand and hosthand < 22:
print('Host Wins!')
print('Reducing 100$ from your account')
playername.banktotal -= 100
break
if hosthand < playerhand or hosthand > 21:
print (str(nameofplayer) + ' Wins!')
print('Adding 100$ to your account')
playername.banktotal += 100
playername = Player(1000)
def game():
print ('Your bank balance: ' + str(playername.banktotal))
rolldice()
something = input('Enter anything for the host to start his turn')
print (something)
print('Host Chance')
hostchance()
print ('Your bank balance: ' + str(playername.banktotal))
def replay():
print('Do you want to play again?')
replay = input('Input with yes or no: ').lower()
if replay == 'yes':
game()
elif replay == 'no':
pass
game()
replay()
Rather than doing pass after receiving a no answer, you should exit() or quit().

how to get out of my game while game ending conditions are not met?

this is my first attempt at python coding, or any coding for that matter.
I have made this simple little game, and it seems to be running fine but I want to add another option to it.
the code generate a random character with HP, attack power, XP and level, then generates a dragon with HP and attack power, the game then decides each time who strikes, and if the player wins he gets to get some Xp and level up, if the dragon wins the player is dead and it asks you to play again.
what I want to add is what if I'm in the middle of a fight, and don't want to continue, I want ask the user if they want to continue fighting, if not the game ends.
I've tried to do that but I failed.
Also , if there is anything I can do to enhance my code.
thanks in advance.
import random
def charGen():
char = [random.randint(1,10),random.randint(1,3), 0, 0]#[hp, power,xp,level]
return char
def drgnGen():
drgn = [random.randint(1,5),random.randint(1,5)]
return drgn
def playAgain():
print('do you want to play again?(y)es or no')
return input().lower().startswith('y')
def xpValues(levels):
for i in range(levels):
n=0
n=((i+2)**2)
xpLevels.append(n)
def xpIncrement(XP,xpLevels,char):
#returns the level of the character( the bracket in which the character XP level lies within)
#level = char[3]
for i in range(len(xpLevels)):
if XP>= xpLevels[i] and XP<xpLevels[i+1]:
#level = i+1
return i
def levelUp(char,level):
if level+1>char[3]:
char[0] += 1
char[3] += 1
print ('you are now at level %s!, your health now is %s points'%((level+1),char[0]))
def isNotDead(char):
if char[0]>0:
return True
else:
return False
while True:
XP = 5 #the default XP gain after battle win
char = charGen() #generate the character
xpLevels=[]
xpValues(15)
print (xpLevels)
print ('______________________________________')
print ('Welcome to the Battle of the dragons!')
print ("you are a fierce Warrior with %s health points and A power of %s points" %(char[0],char[1]))
print ('------------------------------------------------------------------------')
while isNotDead(char):
print(' ')
print ('While adventuring you have met a scary looking dragon')
print('Without hesitation you jump to fight it off!')
print('=============================================')
print(' ')
drgn = drgnGen() #generate a dragon
while True:
roll = random.randint(0,1)
if roll == 0:
print("the dragon hits you for %s points" %drgn[1])
char[0] = char[0] - drgn[1]
if isNotDead(char) :
print("you have %s health left!" %char[0])
input('Press Enter to continue')
print(' ')
else:
print("you're dead!Game Over")
print(' ')
break
else:
print("you hit the dragon for %s points"%char[1])
drgn[0] = drgn[0] - char[1]
if drgn[0] >0:
print("the dragon have %s health left!" %drgn[0])
input('Press Enter to continue')
print(' ')
else:
char[2]+= XP
print("Horaay!you have killed the dragon!and your experience points are now %s"%char[2])
levelUp(char,(xpIncrement(char[2],xpLevels,char)))
input('Press Enter to continue')
break
if not playAgain():
break
A quick fix to get what you want is to have a couple of flags marking whether the user is fighting or not. Also you can delay printing output until the end of the innermost loop, to avoid having to repeat too much printing:
new_dragon = True
while new_dragon:
print(' ')
print ('While adventuring you have met a scary looking dragon')
print('Without hesitation you jump to fight it off!')
print('=============================================')
print(' ')
drgn = drgnGen() #generate a dragon
fighting = True
while fighting:
message = []
roll = random.randint(0,1)
if roll == 0:
message.append("the dragon hits you for %s points" %drgn[1])
char[0] = char[0] - drgn[1]
if isNotDead(char) :
message.append("you have %s health left!" %char[0])
else:
message.append("you're dead!Game Over")
fighting = False
new_dragon = False
else:
message.append("you hit the dragon for %s points"%char[1])
drgn[0] = drgn[0] - char[1]
if drgn[0] >0:
message.append("the dragon have %s health left!" %drgn[0])
else:
char[2]+= XP
message.append("Horaay!you have killed the dragon!and your experience points are now %s"%char[2])
levelUp(char,(xpIncrement(char[2],xpLevels,char)))
continue_flag = False
for m in message:
print (m)
print ('')
if fighting:
r = input("Press enter to continue or enter q to quit")
if r is 'q':
fighting = False
To improve the code more generally:
make a Character class and classes for Player and Dragon that inherit shared properties from the Character class
the check for whether to level up can be greatly simplified by just comparing whether new_xp > xpLevels[previous_xp]
the programme will soon get over-complicated if you continue to expand it just using nested while loops. What you want is a single while loop, functions (or a class) for each stage/status of the game, variables that record the game's current status (e.g. any dragons present), and a condition that decides what to do next.
give things clear descriptive names, using classes, e.g. player.power instead of char[2]
E.g. the following...
import random
class Character:
def __init__(self, max_hp, max_power):
self.hp = random.randint(1, max_hp)
self.power = random.randint(1, max_power)
def is_dead(self):
return self.hp <= 0
def hit_by(self, enemy):
self.hp -= enemy.power
class Player(Character):
def __init__(self):
Character.__init__(self, max_hp=10, max_power=3)
self.xp = 0
self.level = 0
self.xp_thresholds = [(i + 2) ** 2 for i in range(15)]
def battle_win(self):
self.xp += battle_win_xp
if self.level < len(self.xp_thresholds) and self.xp > self.xp_thresholds[self.level + 1]:
self.level_up()
def level_up(self):
self.hp += 1
self.level += 1
print('you are now at level %s!, your health now is %s points' % (self.level + 1, self.hp))
def begin():
game.player = Player()
print('______________________________________')
print('Welcome to the Battle of the dragons!')
print("you are a fierce Warrior with %s health points and A power of %s points" %(game.player.hp, game.player.power))
print('------------------------------------------------------------------------')
def new_dragon():
print('While adventuring you have met a scary looking dragon')
print('Without hesitation you jump to fight it off!')
print('=============================================')
game.dragon = Character(5, 5)
def fight():
player, dragon = game.player, game.dragon
if random.randint(0, 1):
player.hit_by(dragon)
print("the dragon hits you for %s points" % dragon.power)
if player.is_dead():
print("you're dead! Game over")
return
else:
print("you have %s health left!" % player.hp)
else:
dragon.hit_by(player)
print("you hit the dragon for %s points" % player.power)
if dragon.is_dead():
print("Horaay!you have killed the dragon!and your experience points are now %s"%player.xp)
player.battle_win()
game.dragon = None
return
else:
print ("the dragon have %s health left!" %dragon.hp)
print "Press enter to continue (q to quit)"
if input() is 'q':
game.finished = True
def play_again():
print 'do you want to play again?(y)es or no'
if input().lower().startswith('y'):
game.__init__()
else:
game.finished = True
battle_win_xp = 5 #the default XP gain after battle win
class Game:
def __init__(self):
self.dragon = None
self.player = None
self.finished = False
game = Game()
while not game.finished:
if not game.player:
begin()
elif game.player.is_dead():
play_again()
elif not game.dragon:
new_dragon()
else:
fight()

python integrating code with raw_input timeout

This is most of my code for my now working python blackjack game (or at least blackjack like game) I have now been told that I need to implement a time limit (user gets asked to input something and only has 3 or so seconds to give a response).
def deck():
cards = range(1, 12)
return choice(cards)
def dealer():
total = deck() + deck()
if total <= 15:
totalf = total + deck()
while totalf <= 21:
return totalf
if total > 15:
return total
def player():
card1 = deck()
card2 = deck()
hand = card1 + card2
print "Cards dealt: %d and %d" % (card1, card2)
while hand <= 21:
choice = raw_input("Would you like to hit or stand?: ")
print choice
if choice == "hit":
hand = hand + deck()
print "Current Total: %d" % hand
elif choice == "stand":
return hand
money = 100
highscore = 0
while money > 0:
opp = dealer()
me = player()
if me > opp:
highscore = highscore + 10
money = money + 10
print "Winner, winner, chicken dinner! You have $%d!" % money
print "********************************************"
elif opp > 21:
highscore = highscore + 10
money = money + 10
print "Winner, winner, chicken dinner! You have $%d!" % money
print "********************************************"
elif me > 21:
money = money - 20
print "Bust! Dealer wins with %d. You have $%d reamaining." % (opp, money)
print "********************************************"
elif opp > me:
money = money - 20
print "Dealer wins with %d. You have $%d reamaining." % (opp, money)
print "********************************************"
elif me == 21:
highscore = highscore + 10
money = money + 10
print "Blackjack! You have $%d!" % money
print "********************************************"
sleep(1)
print "Thank you for playing! Your highscore was $%d." % highscore
This is the code my professor has provided us with to do this:
import sys, time
from select import select
import platform
if platform.system() == "Windows":
import msvcrt
def input_with_timeout_sane(prompt, timeout, default):
"""Read an input from the user or timeout"""
print prompt,
sys.stdout.flush()
rlist, _, _ = select([sys.stdin], [], [], timeout)
if rlist:
s = sys.stdin.readline().replace('\n','')
else:
s = default
print s
return s
def input_with_timeout_windows(prompt, timeout, default):
start_time = time.time()
print prompt,
sys.stdout.flush()
input = ''
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13: # enter_key
break
elif ord(chr) >= 32: #space_char
input += chr
if len(input) == 0 and (time.time() - start_time) > timeout:
break
if len(input) > 0:
return input
else:
return default
def input_with_timeout(prompt, timeout, default=''):
if platform.system() == "Windows":
return input_with_timeout_windows(prompt, timeout, default)
else:
return input_with_timeout_sane(prompt, timeout, default)
I am completely lost how to merge these two pieces of code. I've tried for the past couple hours to get it to work but for whatever reason its just not working. Any help would be amazing. (I apologize for the wall of code).
You just need to call input_with_timeout function where you want the user's input.
In your player function:
def player():
card1 = deck()
card2 = deck()
hand = card1 + card2
print "Cards dealt: %d and %d" % (card1, card2)
while hand <= 21:
choice = input_with_timeout("Would you like to hit or stand?: ", 3, "stand")
print choice
if choice == "hit":
hand = hand + deck()
print "Current Total: %d" % hand
elif choice == "stand":
return hand
will prompt for an input, writing the "Would ... or stand" sentence before it. If the user do not answer before the timeout (in this case 3 second) the function will return "stand".
And be sure to include your professor's code in your main file.

Categories