Text based game [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
"#TextBasedGame
import random
import time
attackers=random.randint(0,100)
val1=random.randint(0,1)
health=100
bear=100
name=input("What is your name hero? ")
print("Okay ",name," i need to teach you the basics of combat")
time.sleep (1)
print ("Bear spawns")
if val1==0:
print ("Bear attacks -25 HP")
health=health -25
print ("Health = 75")
attack=int(input("Press 1 to attack "))
if attack==1:
print ("Bears HP -25")
bear = bear -25
else:
print ("Why didn't you attack?!?!?")
if bear==100:
print ("Bear =100")
else:
print ("Bear =75")"
If anyone could help me not do each health part and make it print it after every battle / attack that'd help. Please don't make it too complex this is my homework for school and i only started a few weeks ago.

You had a lot of mistakes in your code.You did not decrease the health properly and also you need to put the health in a continuous loop so that the game continues until someone dies. Here's the corrected code:
import random
import time
attackers=random.randint(0,100)
val1=random.randint(0,1)
health=100
bear=100
name=input("What is your name hero? ")
print("Okay ",name," i need to teach you the basics of combat")
time.sleep (1)
print ("Bear spawns")
while((health or bear) > 0):
if val1==0:
print ("Bear attacks -25 HP")
health -= 25
print("Health = ", health)
attack=int(input("Press 1 to attack "))
if attack==1:
print ("Bears HP -25")
bear -= 25
print("Bears HP = ", bear)
else:
print ("Why didn't you attack?!?!?")
else:
if health < bear:
print("You lost!")
else:
print("You won!")

You can print the bear's health directly by like this:
print("bear's health:", bear)
If that's what you were looking for. You're really vague...

Related

Why is only the first question printed in my quiz and nothing else?

I'm writing a quiz program. However, only the first question is printing. Not even the first sentence.
What am I doing wrong?
Here is the code:
import math
import string
import random
print("Simple Star Wars quiz. (SPOILERS!!!) Made by Andy Chakarov.")
cor1 = "Anakin Skywalker" #What other name does Darth Vader go by? (hint: he said that it doesnt mean anything to him anymore in episode 6 Return of the Jedi)
cor2 = "Ben Solo" #What was the original name of Kylo Ren?
cor3 = "Death Star" #What was the ultimate weapon that was rebuilt 3 times
wrong = 0
g1 = input("What other name does Darth Vader go by? (hint: he said that it doesnt mean anything to him anymore in episode 6 Return of the Jedi): ")
if g1 == cor1:
print("Congratulations! You got the answer right!")
else:
print("That's not true!")
wrong + 1
g2 = input("What was the original name of Kylo Ren?: ")
if g2 == cor2:
print("Congratulations! You got the answer right!")
else:
print("That's not true!")
wrong + 1
g3 = input("What was the ultimate weapon that was rebuilt 3 times?: ")
if g3 == cor3:
print("Congratulations! You got the answer right!")
else:
print("That's not true!")
wrong + 1
print("The quiz is over! You got", wrong, "answers out of 3!")
It works.
Just that you get a 0 score even if you answer all the questions right.
Thats because you've specified wrong in the final result.
Simple Star Wars quiz. (SPOILERS!!!) Made by Andy Chakarov.
What other name does Darth Vader go by? (hint: he said that it doesnt mean anything to him anymore in episode 6 Return of the Jedi): Anakin Skywalker
Congratulations! You got the answer right!
What was the original name of Kylo Ren?: Ben Solo
Congratulations! You got the answer right!
What was the ultimate weapon that was rebuilt 3 times?: Death Star
Congratulations! You got the answer right!
The quiz is over! You got 0 answers out of 3!
And regarding it only printing the 1st sentence..I wonder what tool/IDE you're using to run this? You're probably not seeing an option to enter the input values. Better try running it on something like a Jupyter notebook for example and you can see that option

Making Hangman in Processing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am currently trying to make the game Hangman using Python in Processing. I have the code to run the game, as well as code for some visuals. The problem is combining the code I've written so far. The game code is written in standard Python, while the visuals code is written in Processing. Can someone please help me combine the two and make a working Hangman game in Processing? Thank you in advance.
Code for game
import time
import random
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
print ('')
time.sleep(2)
print ("Start guessing...")
bank = ["ironman", "hulk", "captain america", "black widow", "thor", "hawkeye"]
r = (random.randint(0,5))
word = bank[r]
guesses = ''
turns = 10
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print (char, )
else:
print ("_", )
failed += 1
if failed == 0:
print ("You won")
break
print
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print ("Wrong")
print ("You have", + turns, 'more guesses' )
if turns == 0:
print ("You Lose")
Code for visuals
def setup():
size(250,350)
background(102,204,255)
fill(17,214,11)
rect(0,201,250,350)
def draw():
strokeWeight(2)
line(100,100,100,200)
line(100,100,150,100)
line(150,100,150,125)
line(75,200,125,200)
line(85,200,100,175)
line(115,200,100,175)
You should put it all into Processing. It's much easier to add code to a visualization than to add a visualization to other code. There's also an issue with your Python code: think about what will happen if someone guesses multiple characters at once. What if they guess the entire alphabet?
As for actually making the game, you should try to put your Python code into Processing first. Make the game in Processing, then add the visualization. The current state of your game doesn't really work like a Processing sketch, so try getting yourself into the mindset of Processing (frame-by-frame). Make an animation before doing an interactive back-and-forth game like this.

Need Help determining groups on python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need help fixing (Or rewriting) an algorithm. It works out how many hours you have worked in a day and gives you points based on how long you work. This is what i have so far
Amount = ""
Currency = 0
print("Enter Your Figures in 24 hour clock")
print("")
import os;
from time import sleep
sleep (2)
print("You Currently Have "+Amount+" In Your Account!")
from time import sleep
print("When did you leave work?")
Left = input(": ")
print("When did you arrive at work?")
Arrival = input(": ")
Total_Hours = (int(Left) - int(Arrival))
print("900 = 9:00")
print("You worked "+Total_Hours+"")
if TH 1 < TH <=401:
print("You Have Received 4 Points!")
print("Try to work harder!")
Amount = Currency + 4
print ("Your New Balance is " + str(Amount))
I was wondering how I would put the TH into a group such as if hours worked is between 1-4 hours then output something. and if worked between 5-8 hours output something else etc. Becuase i get a syntax error at if TH **1** < TH <=401:I have found various ways on the internet before coming here but none seem to have worked. Thank you for any help you can provide
Try this:
#!/usr/bin/env python
Amount = ""
Currency = 0
print("Enter Your Figures in 24 hour clock")
print("")
print("You Currently Have " + Amount + " In Your Account!")
print("When did you leave work?")
Left = input(": ")
print("When did you arrive at work?")
Arrival = input(": ")
Total_Hours = (int(Left) - int(Arrival))
print("900 = 9:00")
print("You worked %s." % Total_Hours)
if 1 < Total_Hours <= 401:
print("You Have Received 4 Points!")
print("Try to work harder!")
Amount = Currency + 4
print("Your New Balance is " + str(Amount))
TH did not exist but Total_Hours does...
By the way you should read PEP8 (https://www.python.org/dev/peps/pep-0008/)
Just use an if-else statement instead:
if total_hours < 4:
print('You worked less than 4 hours, try to be more productive')
elif total_hours <= 8:
print('You did just enough')
else:
print('Way to go, more than 8 hours today')

updating a global list python

Full disclosure -- this is my first stack overflow question. I searched around for a similar question for awhile but couldn't find one that helped me given my limited scope of knowledge (there were python global variable questions but the answers given were over my head).
I'm new to programming and i'm learning python via python the hard way by Zed Shaw. I'm on exercise36 and my goal is to write my own text based game.
The game starts with the following function
def start():
global Your_HP
Your_HP = 20
global Your_Ammo
Your_Ammo = 20
global HP_Scan
HP_Scan = 2
global your_stats
your_stats = [Your_HP, Your_Ammo, HP_Scan]
print "You enter a room with two doors"
print "Do you choose the Red Door\n...or the Blue door?"
start_choice = raw_input("> ")
if "red" or "Red" in start_choice:
monster_HP = 2
monster_attack = 3
red_one(your_stats, monster_attack, monster_HP)
The red_one function looks like this:
def red_one(your_stats, monster_attack, monster_HP):
print "Immediately you notice a monster in the room"
print "You point your rifle at the monster before you"
shoot(Your_Ammo, monster_HP)
if monster_HP <= 0:
print "You killed it!"
print "You now have %d bullets left" % Your_Ammo
print "You finally look around the room"
print "There is a door going straight"
print "There is a door going left"
room_choose()
elif monster_HP > 0:
print "The monster has %d HP" % monster_HP
print "He stumbles, but not dead!"
print "He shoots back at you with his lil pistol!"
get_shot(monster_attack, Your_HP)
else:
dead('just because')
The script immediately runs the shoot function which looks like:
def shoot(Your_Ammo, monster_HP):
print "---------"
print "You have %d HP left" % Your_HP
print "How many shots do you fire?"
shots_fired = raw_input('> ')
shots_fired = int(shots_fired)
Your_Ammo -= shots_fired
monster_HP -= shots_fired
return Your_Ammo, monster_HP
The problem is that when I run the script and shoot only 1 time, when I return to the start of red_one, the Your_Ammo and monster_HP variables revert to their original values instead of the updated one i'm returning from shoot
I've tried moving where I'm declaring the global variables (initially declaring outside of all functions instead of in start) and I just get an "x variable is local and global" error.
I know global variables in general are not always the best idea, so I'm just hoping someone can break this down for me in an elegant way and provide a helpful solution, regardless of whether or not it includes a global variable.
Ideally, I'd like to update the variables in the your_stats list throughout the course of the game. Essentially, from any function in the game, I want to be able to return any variable within the your_stats list so I can use that variable as an argument for other functions until the player runs out of HP or ammo.
Sorry this post was a bit verbose! Any help is really appreciated!

PYTHON: A "Gambling" program. Can't change value of a variable? (v. 2.7) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
What I was trying do do was make "cash" like a balance. Therefore I want to be able to change that value. But when I run the program, the value of variable "cash" doesn't seem to change when I subtracted from it. The player is supposed to lose cash when they guess wrongly on the dice roll. More info is in the code itself. It would be much appreciated if answers have a brief explaination, im doing this to learn, its purely constructive.
print ""
import time
cash = 5000
print 'you brought','$',cash,'today'
while cash>0:
from random import randint
die = randint(1,1)
while True:
try:
print
choice1 = int(raw_input('First guess: '))
print
choice2 = int(raw_input('Second guess: '))
print
break
except ValueError:
print 'Please, enter a number.'
print 'rolling die..'
time.sleep(3)
if choice1+choice2==die:
#PROBLEM: The operation below does not change the value of cash, why not?.
cash=cash+1000
print cash
print 'you rolled',die
print 'win! you won $1000, you\'re new balance is:',cash
#PROBLEM: The new val of cash should be printed here ^ but it remains as 5000
else:
cash-1000
print 'you rolled',die
print 'lose! you lost $1000, you\'re new balance is:',cash
if cash<0:
print 'Bankrupt.'
time.sleep(3)
quit()
if cash==1000000:
print 'Millionaire!'
break
cash-1000
Here you perform a subtraction, then throw the result away. What you want instead is:
cash = cash - 1000
Or just:
cash -= 1000
cash - 1000
should be
cash -= 1000
Otherwise you aren't assigning cash - 1000 to anything; just evaluating it
In line 28, where you typed cash-1000, you are not changing the cash variable. It should be cash = cash - 1000 or simply cash -= 1000.
Other problems in your code:
Line 6: Don't import a module multiple times. This slows down the program.
Line 20: You are adding the two choices together, but I'm pretty sure you want to check both of them if they equal die. Do this by typing if choice1 == die or choice2 == die: instead. You expected cash to be printed by that line as you commented in line 26, but it never will because the if statement is flawed.

Categories