Making Hangman in Processing [closed] - python

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.

Related

Need a code to allow the user to restart my python quiz at any time [closed]

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 5 years ago.
Improve this question
I would like to know how I could allow my code to let the user restart the quiz at any time throughout the program however, I have no idea how to do this from scratch. It preferably needs to be fairly simple. Would it be easiest doing it as an if statement and if so what would I put in it? I tried this:
while True:
# main program
while True:
answer = raw_input('Run again? (y/n): ')
if answer in ('y', 'n'):
break
print 'Invalid input.'
if answer == 'y':
continue
else:
print 'Goodbye'
break
breaking your problem down into parts is the first step
# 1. ask a question and validate the response
def ask_question(prompt,possible_choices):
while True:
result = raw_input(prompt)
if result in possible_choices:
return result
print "Invalid Response Please Enter One Of:",possible_choices
# 2. run a whole quiz (ask all our questions)
def do_quiz():
answers = []
for question_text in questions_list:
answer = ask_question(question_text,['a','b','c','d','quit','restart')
if answer == "restart":
return False
# instead of returning false we could also simply call recursively
# return do_quiz()
elif answer == "quit":
print "Adios muchacho"
sys.exit(0)
else:
answers.append(answer)
return answers
# 3. play forever (or atleast until the user wants to quit...)
while True:
results = do_quiz()
if not results:
print "Lets Start Over!!"
continue
else:
check_answers(results)
if raw_input("Play again?")[0].lower() == "n":
print "Goodbye!"
sys.exit(1)

Why does my while loop not stop? [closed]

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 5 years ago.
Improve this question
RANDOM_COR=random.randrange(5,6)
def check_xy_data():
global COUNT
COUNT=0
input_xy=input("input(x,y) : ")
think_xy=list(map(int,input_xy.split(",")))
if(random_array[think_xy[0]][think_xy[1]] == "C"):
screen_array[think_xy[0]][think_xy[1]] = "O"
COUNT=COUNT+1
else:
screen_array[think_xy[0]][think_xy[1]] = "X"
def main():
make_intro()
init_screen_array ()
init_random_array ()
make_random_num(RANDOM_COR)
while(True):
check_xy_data()
draw_outline_start(TOTAL_COL_NUM//2)
draw_out_rowline(TOTAL_COL_NUM//2, "Input : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Correct : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Error : ")
draw_out_rowline(TOTAL_COL_NUM//2, "Total : ")
draw_outline_mid(TOTAL_COL_NUM//2)
if(COUNT==RANDOM_COR-1):
break
The if at the bottom of my code is supposed to get me out of the while loop, but I'm stuck in an infinite loop. Help?
(assignment, 2016) 예고편 The Assignment | 어싸인먼트 감독: 월터 힐 각본: 월터 힐, 데니스 해밀 출연: 김성훈 출연 현빈, 유해진, 김주혁 개봉 2016 한국 상세보기 그간...
Try this change:
RANDOM_COR=random.randrange(5,6)
COUNT = 0
def check_xy_data():
global COUNT
With COUNT inside check_xy_data, you set it back to 0 on every call. It can never reach more than 1. Your check is whether it's in the range 5-6. This is never true, so you can never leave the loop.
Note that trivial debugging skills would have found this: just stick a print statement before you test your loop condition, to see what the values are. Use that next time ... :-)

Text based game [closed]

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...

Newbie query about conditional statements [closed]

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
I'm learning Python and can't work out why the following doesn't work.
Can anyone advise? code below
thanks
# Make sure that the_flying_circus() returns True
print "What is your number?"
num = input()
print "What is bla?"
bla = input()
def the_flying_circus():
if num ==4 and bla=="bla": # Start coding here!
return True
print "Well done!"
# Don't forget to indent
# the code inside this block!
elif num == 2 or bla== zog:
print "OK"
# Keep going here.
# You'll want to add the else statement, too!
else:
print "Bad luck!"
the_flying_circus()
The return True is probably not what you want to have on the top of the if block. Try removing it.
The only condition that will return True is num==4 and bla=='bla'. Otherwise, the return value is None. However, 'Well done!' will never be printed since the return statement occurs first.
Couple of things...
1) return True should be moved to the end of the function (as mentioned by others)
2) watch how you collect input... use raw_input for your string, use input for the number.
This works for me:
def the_flying_circus():
if a==4 and b=='bla':
print "Well done!"
elif a==2 or b=="zog":
print "OK"
else:
print "Bad luck!"
return 1
a = input("What is your number? ")
b = raw_input("What is bla? ")
the_flying_circus()

How do Arguments and Parameters Work in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have looked all over Stackoverflow and I can't find an answer, and all the web tutorials just go right over my head. I have a functioning code that I don't understand
import random
import time
def displayIntro():
print('You are in a land full of dragons. In front of you,')
print('you see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with you. The other dragon')
print('is greedy nd hungry, and will eat you on sight.')
print()
def chooseCave():
cave = ''
while cave != '1' and cave != '2':
print('Which cave will you go into? (1 or 2)')
cave = input()
return cave
def checkCave(chosenCave):
print('You approach the cave...')
time.sleep(2)
print('It is dark and spooky...')
time.sleep(2)
print('A large dragon jumps out in front of you! He opens his jaws and...')
print()
time.sleep(2)
friendlyCave = random.randint(1, 2)
if chosenCave == str(friendlyCave):
print('Gives you his treasure')
else:
print('Gobbles you down in one bite!')
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
displayIntro()
caveNumber = chooseCave()
checkCave(caveNumber)
print('do you want to play again? (yes or no)')
playAgain = input()
I don't understand the def checkCave(chosenCave): parts, why does the argument say chosenCave?
Can someone explain please?
In the function
def checkCave(chosenCave):
...
chosenCave becomes a local variable that you passed to the function. You can then access the value inside that function to process it, provide whatever side-effects you're looking to provide (like printing to the screen, as you're doing), and then return a value, (which if you don't do explicitly, Python returns None, its null value, by default.)
Algebraic Analogy
In algebra we define functions like this:
f(x) = ...
for example:
f(x) = x*x
In Python we define functions like this:
def f(x):
...
and in keeping with the above simple example:
def f(x):
return x*x
When we want the results of that function applied to a particular x, (e.g., 1), we call it, and it returns the result after processing that particular x.:
particular_x = 1
f(particular_x)
And if it returns a result we want for later usage, we can assign the results of calling that function to a variable:
y = f(particular_x)
The name chosenCave appears to have been chosen to describe what it represents, namely, the cave the player chose. Were you expecting it to be named something else? The name isn't required to match or not match any names located elsewhere in the program.

Categories