Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59282 > unrolled thread
| Started by | saad imran <saad.imran98@gmail.com> |
|---|---|
| First post | 2013-11-12 20:18 -0800 |
| Last post | 2013-11-13 13:30 +0100 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.python
Please help with this saad imran <saad.imran98@gmail.com> - 2013-11-12 20:18 -0800
Re: Please help with this Gary Herron <gary.herron@islandtraining.com> - 2013-11-12 20:34 -0800
Re: Please help with this Ben Finney <ben+python@benfinney.id.au> - 2013-11-13 16:54 +1100
Re: Please help with this Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-13 06:02 +0000
Re: Please help with this Dave Angel <davea@davea.name> - 2013-11-13 00:45 -0600
Re: Please help with this Steven D'Aprano <steve@pearwood.info> - 2013-11-13 06:49 +0000
Re: Please help with this mkharper <mkharper@gmail.com> - 2013-11-13 00:53 -0800
Re: Please help with this mkharper <mkharper@gmail.com> - 2013-11-13 01:02 -0800
Re: Please help with this mkharper <mkharper@gmail.com> - 2013-11-13 03:09 -0800
Re: Please help with this Peter Otten <__peter__@web.de> - 2013-11-13 13:30 +0100
| From | saad imran <saad.imran98@gmail.com> |
|---|---|
| Date | 2013-11-12 20:18 -0800 |
| Subject | Please help with this |
| Message-ID | <6ade7311-fe93-4747-959b-e9b947714b00@googlegroups.com> |
Could you point out any errors in my code:
#This is a game where you have to escape a dragon.
# By Saad Imran
import random
import pygame
# Define questions and answers.
que1 = "4481 *2"
ans1 = "8962"
que2 = "457 * 21"
ans2 = "9597"
que3 = "2345*23"
ans3 = "53935"
que4 = "Who plays against the USA in golf's Walker Cup?"
ans4 = "britain"
que5 = "Which game is played in autumn using the fruit of the horse chestnut tree?"
ans5 = "conkers"
que6 = "From what country does Lego come?"
ans6 = "denmark"
que7 = "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?"
ans7 = "umpire"
que8 = "What date was D-Day?"
ans8 = "06,06,1944"
que9 ="Who is called the 'Great One' in hockey history?"
ans9 = "wayne gretzky"
# intro
print("Welcome!")
print("")
name = input("What is your name? ")
print("")
print("You are in a cave and hear a dragon!",name)
print("")
print("Thinking you are doomed, you panic and run")
print("")
# Ask whether user wants to play or not
print("You run into a wizard who tells you that he will teleport you out of the cave if you solve his questions!")
print("")
game = input("Do you trust him?(y/n) ")
print("")
#if he doesn't game is ended
if game == "N" or game == "n":
print("The dragon catches you and you are doomed!")
gameloop = False
loop = False
# if he does, game starts
else:
gameLoop = True
while gameLoop ==True:
# Tell user how to write answers
print("Please use all lower case letters. date format is dd,mm,yyyy")
print("dragon is red, You are green!")
# The following code intialises a pygame window where user can track progress of self and dragon
# Needed to initialize pygame
pygame.init()
# define variable for user position
userX =75
userY =450
# define variable for dragon position.
dragonX = 0
dragonY = 450
# Define some Constants(colours and screen size)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
# Create a screen
size = [SCREEN_WIDTH,SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("dragonEscape Game")
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#Loop until the user clicks the close button.
loop = True
# -------- Main Program Loop -----------
while loop == True:
# EVENT PROCESSING
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
#if user is caught
if userX <= dragonX:
print("The dragon ate you for dinner!")
loop = False
gameLoop = False
#if user wins
if userX >= 680:
print("You win!")
loop = False
gameLoop = False
# DRAW COMMANDS
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, [userX,userY,50,50])
pygame.draw.rect(screen, RED, [dragonX,dragonY,50,50])
pygame.draw.line(screen, BLACK, [680,0],[680,500],40)
#Code to generate random number
questionSelection = random.randrange(1,10)
# if statements that select question randomly based on random number generated and collects user answer
print("")
print("")
print("")
print("")
print("")
print("")
if questionSelection == 1:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans1:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 2:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans2:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 3:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans3:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 4:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans4:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 5:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans5:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 6:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans6:
print("Correct!")
userX +=75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 7:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans7:
print("Correct!")
userX += 75
else:
print("UH-OH!")
dragonX += 75
elif questionSelection == 8:
print(que1)
playerAnswer = input("What do you think the answer is?")
if playerAnswer ==ans8:
print("Correct!")
userX += 76
else:
print("UH-OH!")
dragonX += 75
else:
print(que9)
playerAnswer = input("What do you think the answer is?")
if playerAnswer == ans9:
print("Correct!")
userX += 25
else:
print("UH-OH!")
dragonX += 75
print("")
print("")
print("")
print("")
print("")
# Update Screen
pygame.display.flip()
# Set frames per second
clock.tick(20)
# -------- End of Main Program Loop --------
# Close the window and quit.
pygame.quit()
#game over, ending remarks.
print("Thanks for playing!")
[toc] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2013-11-12 20:34 -0800 |
| Message-ID | <mailman.2523.1384317699.18130.python-list@python.org> |
| In reply to | #59282 |
On 11/12/2013 08:18 PM, saad imran wrote: > Could you point out any errors in my code: > > <Snip 280+ lines of code.> Nope. You've got to do *some* of the work if you expect free volunteer help from people around here. Take the time to tell us what you expect this program to do, what actually happens when you run it, what errors you get. If you get Python traceback, cut and paste it into an email. Tell us how you have tried to fix the problem, whatever that may be. Give some hint where in the nearly 300 lines of code the problem may be occurring. You can get lots of free volunteer help from this group, but the question, as you've asked it, is a misuse (or even an *abuse*) of this group. Gary Herron
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2013-11-13 16:54 +1100 |
| Message-ID | <mailman.2525.1384322089.18130.python-list@python.org> |
| In reply to | #59282 |
saad imran <saad.imran98@gmail.com> writes: > Could you point out any errors in my code: This isn't a code review service; it is a community discussion forum. Do you have specific Python-related questions? What particular problems are you having with this code? -- \ “Pinky, are you pondering what I'm pondering?” “I think so, | `\ Brain, but shouldn't the bat boy be wearing a cape?” —_Pinky | _o__) and The Brain_ | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-13 06:02 +0000 |
| Message-ID | <mailman.2526.1384322542.18130.python-list@python.org> |
| In reply to | #59282 |
On 13/11/2013 04:34, Gary Herron wrote: > On 11/12/2013 08:18 PM, saad imran wrote: >> Could you point out any errors in my code: >> >> > <Snip 280+ lines of code.> > > Nope. > > > > You've got to do *some* of the work if you expect free volunteer help > from people around here. Take the time to tell us what you expect this > program to do, what actually happens when you run it, what errors you > get. If you get Python traceback, cut and paste it into an email. Tell > us how you have tried to fix the problem, whatever that may be. Give > some hint where in the nearly 300 lines of code the problem may be > occurring. > > You can get lots of free volunteer help from this group, but the > question, as you've asked it, is a misuse (or even an *abuse*) of this > group. > > Gary Herron > > It's my belief that the above is way OTT given the dross we've had to put up with from a certain Greek individual over the last few months. Why isn't the OP here being spoon fed? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-11-13 00:45 -0600 |
| Message-ID | <mailman.2527.1384325096.18130.python-list@python.org> |
| In reply to | #59282 |
On Tue, 12 Nov 2013 20:18:58 -0800 (PST), saad imran <saad.imran98@gmail.com> wrote: > Could you point out any errors in my code: > que1 = "4481 *2" > ans1 = "8962" > que2 = "457 * 21" > ans2 = "9597" These values should all be in a single named structure, probably a list of tuples. Then all that duplicated code could be condensed into a single block. Is that the error you wanted us to find? -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2013-11-13 06:49 +0000 |
| Message-ID | <528320df$0$29980$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #59282 |
On Tue, 12 Nov 2013 20:18:58 -0800, saad imran wrote:
> Could you point out any errors in my code:
Hi Saad, and welcome!
As others have pointed out, you'll have better results if you tell us
what errors you have, what result you actually expected, and what you
have tried to do to fix it.
If you just want a general code review, it is best to make that clear by
saying you have no known errors.
Also, unfortunately not everyone here is familiar with PyGame. For PyGame
questions, you may be better asking for help on a PyGame forum or mailing
list. We'll help if we can, but we're not experts.
I haven't gone through your entire program in detail, but one small thing
stands out:
> # intro
> print("Welcome!")
> print("")
You don't need to print the empty string to get a blank like. You can
either call print with no arguments:
print("Welcome!")
print()
or you can manually add an extra newline to the string:
print("Welcome!\n")
Hope this helps,
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | mkharper <mkharper@gmail.com> |
|---|---|
| Date | 2013-11-13 00:53 -0800 |
| Message-ID | <ac98c2e1-c3a0-4080-9ac6-626706e417f4@googlegroups.com> |
| In reply to | #59282 |
On Wednesday, November 13, 2013 4:18:58 AM UTC, saad imran wrote:
> Could you point out any errors in my code:
>
>
>
> #This is a game where you have to escape a dragon.
>
> # By Saad Imran
>
>
>
> import random
>
> import pygame
>
>
>
>
>
>
>
> # Define questions and answers.
>
> que1 = "4481 *2"
>
> ans1 = "8962"
>
> que2 = "457 * 21"
>
> ans2 = "9597"
>
> que3 = "2345*23"
>
> ans3 = "53935"
>
> que4 = "Who plays against the USA in golf's Walker Cup?"
>
> ans4 = "britain"
>
> que5 = "Which game is played in autumn using the fruit of the horse chestnut tree?"
>
> ans5 = "conkers"
>
> que6 = "From what country does Lego come?"
>
> ans6 = "denmark"
>
> que7 = "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?"
>
> ans7 = "umpire"
>
> que8 = "What date was D-Day?"
>
> ans8 = "06,06,1944"
>
> que9 ="Who is called the 'Great One' in hockey history?"
>
> ans9 = "wayne gretzky"
>
>
>
>
>
> # intro
>
> print("Welcome!")
>
> print("")
>
> name = input("What is your name? ")
>
> print("")
>
> print("You are in a cave and hear a dragon!",name)
>
> print("")
>
> print("Thinking you are doomed, you panic and run")
>
> print("")
>
>
>
> # Ask whether user wants to play or not
>
> print("You run into a wizard who tells you that he will teleport you out of the cave if you solve his questions!")
>
> print("")
>
> game = input("Do you trust him?(y/n) ")
>
> print("")
>
>
>
> #if he doesn't game is ended
>
> if game == "N" or game == "n":
>
> print("The dragon catches you and you are doomed!")
>
> gameloop = False
>
> loop = False
>
> # if he does, game starts
>
>
>
> else:
>
> gameLoop = True
>
>
>
> while gameLoop ==True:
>
>
>
> # Tell user how to write answers
>
>
>
> print("Please use all lower case letters. date format is dd,mm,yyyy")
>
> print("dragon is red, You are green!")
>
>
>
>
>
>
>
> # The following code intialises a pygame window where user can track progress of self and dragon
>
> # Needed to initialize pygame
>
> pygame.init()
>
>
>
>
>
>
>
>
>
>
>
> # define variable for user position
>
> userX =75
>
> userY =450
>
>
>
>
>
>
>
> # define variable for dragon position.
>
> dragonX = 0
>
> dragonY = 450
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> # Define some Constants(colours and screen size)
>
> BLACK = (0, 0, 0)
>
> WHITE = (255, 255, 255)
>
> GREEN = (0, 255, 0)
>
> RED = (255, 0, 0)
>
>
>
> SCREEN_WIDTH = 700
>
> SCREEN_HEIGHT = 500
>
>
>
> # Create a screen
>
> size = [SCREEN_WIDTH,SCREEN_HEIGHT]
>
> screen = pygame.display.set_mode(size)
>
>
>
> pygame.display.set_caption("dragonEscape Game")
>
>
>
> # Used to manage how fast the screen updates
>
> clock = pygame.time.Clock()
>
>
>
> #Loop until the user clicks the close button.
>
> loop = True
>
>
>
> # -------- Main Program Loop -----------
>
> while loop == True:
>
> # EVENT PROCESSING
>
> for event in pygame.event.get():
>
> if event.type == pygame.QUIT:
>
> loop = False
>
>
>
> #if user is caught
>
>
>
> if userX <= dragonX:
>
> print("The dragon ate you for dinner!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
> #if user wins
>
> if userX >= 680:
>
>
>
> print("You win!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
>
>
> # DRAW COMMANDS
>
> screen.fill(WHITE)
>
>
>
>
>
> pygame.draw.rect(screen, GREEN, [userX,userY,50,50])
>
> pygame.draw.rect(screen, RED, [dragonX,dragonY,50,50])
>
> pygame.draw.line(screen, BLACK, [680,0],[680,500],40)
>
>
>
>
>
> #Code to generate random number
>
>
>
> questionSelection = random.randrange(1,10)
>
>
>
>
>
> # if statements that select question randomly based on random number generated and collects user answer
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> if questionSelection == 1:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans1:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 2:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans2:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
> elif questionSelection == 3:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans3:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 4:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans4:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 5:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans5:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 6:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans6:
>
> print("Correct!")
>
> userX +=75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 7:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans7:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
> elif questionSelection == 8:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans8:
>
> print("Correct!")
>
> userX += 76
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> else:
>
> print(que9)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer == ans9:
>
> print("Correct!")
>
> userX += 25
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
>
>
>
>
>
>
>
>
> # Update Screen
>
> pygame.display.flip()
>
>
>
> # Set frames per second
>
> clock.tick(20)
>
> # -------- End of Main Program Loop --------
>
>
>
>
>
> # Close the window and quit.
>
> pygame.quit()
>
>
>
>
>
> #game over, ending remarks.
>
> print("Thanks for playing!")
Hi,
You have and odd "gameloop". All others are "gameLoop".
Michael
[toc] | [prev] | [next] | [standalone]
| From | mkharper <mkharper@gmail.com> |
|---|---|
| Date | 2013-11-13 01:02 -0800 |
| Message-ID | <d3d811dd-a44e-4583-8cd4-2b376890f7b6@googlegroups.com> |
| In reply to | #59282 |
On Wednesday, November 13, 2013 4:18:58 AM UTC, saad imran wrote:
> Could you point out any errors in my code:
>
>
>
> #This is a game where you have to escape a dragon.
>
> # By Saad Imran
>
>
>
> import random
>
> import pygame
>
>
>
>
>
>
>
> # Define questions and answers.
>
> que1 = "4481 *2"
>
> ans1 = "8962"
>
> que2 = "457 * 21"
>
> ans2 = "9597"
>
> que3 = "2345*23"
>
> ans3 = "53935"
>
> que4 = "Who plays against the USA in golf's Walker Cup?"
>
> ans4 = "britain"
>
> que5 = "Which game is played in autumn using the fruit of the horse chestnut tree?"
>
> ans5 = "conkers"
>
> que6 = "From what country does Lego come?"
>
> ans6 = "denmark"
>
> que7 = "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?"
>
> ans7 = "umpire"
>
> que8 = "What date was D-Day?"
>
> ans8 = "06,06,1944"
>
> que9 ="Who is called the 'Great One' in hockey history?"
>
> ans9 = "wayne gretzky"
>
>
>
>
>
> # intro
>
> print("Welcome!")
>
> print("")
>
> name = input("What is your name? ")
>
> print("")
>
> print("You are in a cave and hear a dragon!",name)
>
> print("")
>
> print("Thinking you are doomed, you panic and run")
>
> print("")
>
>
>
> # Ask whether user wants to play or not
>
> print("You run into a wizard who tells you that he will teleport you out of the cave if you solve his questions!")
>
> print("")
>
> game = input("Do you trust him?(y/n) ")
>
> print("")
>
>
>
> #if he doesn't game is ended
>
> if game == "N" or game == "n":
>
> print("The dragon catches you and you are doomed!")
>
> gameloop = False
>
> loop = False
>
> # if he does, game starts
>
>
>
> else:
>
> gameLoop = True
>
>
>
> while gameLoop ==True:
>
>
>
> # Tell user how to write answers
>
>
>
> print("Please use all lower case letters. date format is dd,mm,yyyy")
>
> print("dragon is red, You are green!")
>
>
>
>
>
>
>
> # The following code intialises a pygame window where user can track progress of self and dragon
>
> # Needed to initialize pygame
>
> pygame.init()
>
>
>
>
>
>
>
>
>
>
>
> # define variable for user position
>
> userX =75
>
> userY =450
>
>
>
>
>
>
>
> # define variable for dragon position.
>
> dragonX = 0
>
> dragonY = 450
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> # Define some Constants(colours and screen size)
>
> BLACK = (0, 0, 0)
>
> WHITE = (255, 255, 255)
>
> GREEN = (0, 255, 0)
>
> RED = (255, 0, 0)
>
>
>
> SCREEN_WIDTH = 700
>
> SCREEN_HEIGHT = 500
>
>
>
> # Create a screen
>
> size = [SCREEN_WIDTH,SCREEN_HEIGHT]
>
> screen = pygame.display.set_mode(size)
>
>
>
> pygame.display.set_caption("dragonEscape Game")
>
>
>
> # Used to manage how fast the screen updates
>
> clock = pygame.time.Clock()
>
>
>
> #Loop until the user clicks the close button.
>
> loop = True
>
>
>
> # -------- Main Program Loop -----------
>
> while loop == True:
>
> # EVENT PROCESSING
>
> for event in pygame.event.get():
>
> if event.type == pygame.QUIT:
>
> loop = False
>
>
>
> #if user is caught
>
>
>
> if userX <= dragonX:
>
> print("The dragon ate you for dinner!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
> #if user wins
>
> if userX >= 680:
>
>
>
> print("You win!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
>
>
> # DRAW COMMANDS
>
> screen.fill(WHITE)
>
>
>
>
>
> pygame.draw.rect(screen, GREEN, [userX,userY,50,50])
>
> pygame.draw.rect(screen, RED, [dragonX,dragonY,50,50])
>
> pygame.draw.line(screen, BLACK, [680,0],[680,500],40)
>
>
>
>
>
> #Code to generate random number
>
>
>
> questionSelection = random.randrange(1,10)
>
>
>
>
>
> # if statements that select question randomly based on random number generated and collects user answer
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> if questionSelection == 1:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans1:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 2:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans2:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
> elif questionSelection == 3:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans3:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 4:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans4:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 5:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans5:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 6:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans6:
>
> print("Correct!")
>
> userX +=75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 7:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans7:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
> elif questionSelection == 8:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans8:
>
> print("Correct!")
>
> userX += 76
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> else:
>
> print(que9)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer == ans9:
>
> print("Correct!")
>
> userX += 25
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
>
>
>
>
>
>
>
>
> # Update Screen
>
> pygame.display.flip()
>
>
>
> # Set frames per second
>
> clock.tick(20)
>
> # -------- End of Main Program Loop --------
>
>
>
>
>
> # Close the window and quit.
>
> pygame.quit()
>
>
>
>
>
> #game over, ending remarks.
>
> print("Thanks for playing!")
Also....
Instead of asking the question 8 times, how about using a couple of dictionaries for the questions and answers with the answers include the coordinates like this:-
QUE = {1: "4481 *2",
2: "457 * 21",
3: "2345*23",
4: "Who plays against the USA in golf's Walker Cup?",
5: "Which game is played in autumn using the fruit of the horse chestnut tree?",
6: "From what country does Lego come?",
7: "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?",
8: "What date was D-Day?",
9: "Who is called the 'Great One' in hockey history?", }
ANS = {1: "8962;75;75",
2: "9597;75;75",
3: "53935;75;75",
4: "britain;75;75",
5: "conkers;75;75",
6: "denmark;75;75",
7: "umpire;75;75",
8: "06,06,1944;76;75",
9: "wayne gretzky;25;75", }
and then just test the question/answer with:
queSel = random.randrange(1, 10)
print("\n"*6)
print(QUE[queSel])
playerAnswer = input("What do you think the answer is?")
if playerAnswer == ANS[queSel].split(';')[0]:
print("Correct!")
USERX += ANS[queSel].split(';')[1]
else:
print("UH-OH!")
DRAGONX += ANS[queSel].split(';')[2]
print("\n"*5)
I'm sure there's better ways. This is just what came to mind.
Regards,
Michael
[toc] | [prev] | [next] | [standalone]
| From | mkharper <mkharper@gmail.com> |
|---|---|
| Date | 2013-11-13 03:09 -0800 |
| Message-ID | <89009d07-acb3-4c62-855b-2a25f0487125@googlegroups.com> |
| In reply to | #59282 |
On Wednesday, November 13, 2013 4:18:58 AM UTC, saad imran wrote:
> Could you point out any errors in my code:
>
>
>
> #This is a game where you have to escape a dragon.
>
> # By Saad Imran
>
>
>
> import random
>
> import pygame
>
>
>
>
>
>
>
> # Define questions and answers.
>
> que1 = "4481 *2"
>
> ans1 = "8962"
>
> que2 = "457 * 21"
>
> ans2 = "9597"
>
> que3 = "2345*23"
>
> ans3 = "53935"
>
> que4 = "Who plays against the USA in golf's Walker Cup?"
>
> ans4 = "britain"
>
> que5 = "Which game is played in autumn using the fruit of the horse chestnut tree?"
>
> ans5 = "conkers"
>
> que6 = "From what country does Lego come?"
>
> ans6 = "denmark"
>
> que7 = "What term is used in cricket for the two men on the field who decide on whether batsmen are out, and signal for extras and boundaries?"
>
> ans7 = "umpire"
>
> que8 = "What date was D-Day?"
>
> ans8 = "06,06,1944"
>
> que9 ="Who is called the 'Great One' in hockey history?"
>
> ans9 = "wayne gretzky"
>
>
>
>
>
> # intro
>
> print("Welcome!")
>
> print("")
>
> name = input("What is your name? ")
>
> print("")
>
> print("You are in a cave and hear a dragon!",name)
>
> print("")
>
> print("Thinking you are doomed, you panic and run")
>
> print("")
>
>
>
> # Ask whether user wants to play or not
>
> print("You run into a wizard who tells you that he will teleport you out of the cave if you solve his questions!")
>
> print("")
>
> game = input("Do you trust him?(y/n) ")
>
> print("")
>
>
>
> #if he doesn't game is ended
>
> if game == "N" or game == "n":
>
> print("The dragon catches you and you are doomed!")
>
> gameloop = False
>
> loop = False
>
> # if he does, game starts
>
>
>
> else:
>
> gameLoop = True
>
>
>
> while gameLoop ==True:
>
>
>
> # Tell user how to write answers
>
>
>
> print("Please use all lower case letters. date format is dd,mm,yyyy")
>
> print("dragon is red, You are green!")
>
>
>
>
>
>
>
> # The following code intialises a pygame window where user can track progress of self and dragon
>
> # Needed to initialize pygame
>
> pygame.init()
>
>
>
>
>
>
>
>
>
>
>
> # define variable for user position
>
> userX =75
>
> userY =450
>
>
>
>
>
>
>
> # define variable for dragon position.
>
> dragonX = 0
>
> dragonY = 450
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> # Define some Constants(colours and screen size)
>
> BLACK = (0, 0, 0)
>
> WHITE = (255, 255, 255)
>
> GREEN = (0, 255, 0)
>
> RED = (255, 0, 0)
>
>
>
> SCREEN_WIDTH = 700
>
> SCREEN_HEIGHT = 500
>
>
>
> # Create a screen
>
> size = [SCREEN_WIDTH,SCREEN_HEIGHT]
>
> screen = pygame.display.set_mode(size)
>
>
>
> pygame.display.set_caption("dragonEscape Game")
>
>
>
> # Used to manage how fast the screen updates
>
> clock = pygame.time.Clock()
>
>
>
> #Loop until the user clicks the close button.
>
> loop = True
>
>
>
> # -------- Main Program Loop -----------
>
> while loop == True:
>
> # EVENT PROCESSING
>
> for event in pygame.event.get():
>
> if event.type == pygame.QUIT:
>
> loop = False
>
>
>
> #if user is caught
>
>
>
> if userX <= dragonX:
>
> print("The dragon ate you for dinner!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
> #if user wins
>
> if userX >= 680:
>
>
>
> print("You win!")
>
> loop = False
>
> gameLoop = False
>
>
>
>
>
>
>
> # DRAW COMMANDS
>
> screen.fill(WHITE)
>
>
>
>
>
> pygame.draw.rect(screen, GREEN, [userX,userY,50,50])
>
> pygame.draw.rect(screen, RED, [dragonX,dragonY,50,50])
>
> pygame.draw.line(screen, BLACK, [680,0],[680,500],40)
>
>
>
>
>
> #Code to generate random number
>
>
>
> questionSelection = random.randrange(1,10)
>
>
>
>
>
> # if statements that select question randomly based on random number generated and collects user answer
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> if questionSelection == 1:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans1:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 2:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans2:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
> elif questionSelection == 3:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans3:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 4:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans4:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 5:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans5:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
>
>
>
>
>
>
> elif questionSelection == 6:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans6:
>
> print("Correct!")
>
> userX +=75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> elif questionSelection == 7:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans7:
>
> print("Correct!")
>
> userX += 75
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
> elif questionSelection == 8:
>
> print(que1)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer ==ans8:
>
> print("Correct!")
>
> userX += 76
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> else:
>
> print(que9)
>
> playerAnswer = input("What do you think the answer is?")
>
> if playerAnswer == ans9:
>
> print("Correct!")
>
> userX += 25
>
> else:
>
> print("UH-OH!")
>
> dragonX += 75
>
>
>
>
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
> print("")
>
>
>
>
>
>
>
>
>
> # Update Screen
>
> pygame.display.flip()
>
>
>
> # Set frames per second
>
> clock.tick(20)
>
> # -------- End of Main Program Loop --------
>
>
>
>
>
> # Close the window and quit.
>
> pygame.quit()
>
>
>
>
>
> #game over, ending remarks.
>
> print("Thanks for playing!")
Hi Saad,
I've had a play and the following "does something".
(I'm running Python 2.7 so replaced input with raw_input.)
(User can use upper or lower case, just lower it before test.)
(I simplified the questions/answers so I could answer them.)
I'm out of time but hope the following helps.
Kind regards,
Michael
#!/usr/bin/env python
"""
This is a game where you have to escape a dragon.
By Saad Imran
"""
import random
import pygame
# Define questions and answers.
QUE = {1: "4 x 2",
2: "3 x 6",
3: "2 x 5",
4: "6 / 2",
5: "7 + 7",
6: "8 - 3",
7: "5 x 5",
8: "4 / 1",
9: "0 x 6", }
ANS = {1: "8;75;75",
2: "18;75;75",
3: "10;75;75",
4: "3;75;75",
5: "14;75;75",
6: "5;75;75",
7: "25;75;75",
8: "4;75;75",
9: "0;75;75", }
# intro
print("Welcome!\n")
NAME = raw_input("What is your name?: ")
print("\nYou are in a cave and hear a dragon %s!" % NAME)
print("Thinking you are doomed, you panic and run")
# Ask whether user wants to play or not
print("You run into a wizard who tells you that he will")
print("teleport you out of the cave if you solve his questions!\n")
GAME = raw_input("Do you trust him? (y/n): ")
#if he doesn't game is ended
if GAME.lower() == "n":
print("The dragon catches you and you are doomed!\n")
GAMELOOP = False
LOOP = False
# if he does, game starts
else:
GAMELOOP = True
while GAMELOOP == True:
print("(Dragon is red, You are green!)\n")
# Following code intialises a pygame window
# where user can track progress of self and dragon.
# Initialize pygame
pygame.init()
# define variable for user position
USERX = 75
USERY = 450
# define variable for dragon position.
DRAGONX = 0
DRAGONY = 450
# Define some Constants(colours and screen size)
BLACK = (0, 0, 0,)
WHITE = (255, 255, 255,)
GREEN = (0, 255, 0,)
RED = (255, 0, 0,)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
# Create a screen
SIZE = [SCREEN_WIDTH, SCREEN_HEIGHT]
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption("dragonEscape Game")
# Used to manage how fast the screen updates
CLOCK = pygame.time.Clock()
# Loop until the user clicks the close button.
LOOP = True
# -------- Main Program Loop -----------
while LOOP:
# EVENT PROCESSING
for event in pygame.event.get():
if event.type == pygame.QUIT:
LOOP = False
# DRAW COMMANDS
SCREEN.fill(WHITE)
pygame.draw.rect(SCREEN, GREEN, [USERX, USERY, 50, 50])
pygame.draw.rect(SCREEN, RED, [DRAGONX, DRAGONY, 50, 50])
pygame.draw.line(SCREEN, BLACK, [680, 0], [680, 500], 40)
pygame.display.flip()
# Code to generate random number
QUESEL = random.randrange(1, 10)
# if statements that select question randomly based on random number generated
# and collects user answer.
print("\n"*3)
print(QUE[QUESEL])
PLAYERANS = raw_input("What do you think the answer is? ")
if PLAYERANS.lower() == ANS[QUESEL].split(';')[0].lower():
print("Correct!")
USERX += int(ANS[QUESEL].split(';')[1])
else:
print("UH-OH!")
DRAGONX += int(ANS[QUESEL].split(';')[2])
print "\n"*2
# Update Screen
pygame.display.flip()
CLOCK.tick(20)
#if user is caught
if USERX <= DRAGONX:
print("The dragon ate you for dinner!")
LOOP = False
GAMELOOP = False
#if user wins
if USERX >= 680:
print("You win!")
LOOP = False
GAMELOOP = False
# Set frames per second
# --------- End of Main Program Loop ---------
# Close the window and quit.
pygame.quit()
#game over, ending remarks.
print "Thanks for playing!"
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-11-13 13:30 +0100 |
| Message-ID | <mailman.2531.1384345789.18130.python-list@python.org> |
| In reply to | #59295 |
mkharper wrote:
> Hi Saad,
>
> I've had a play and the following "does something".
> (I'm running Python 2.7 so replaced input with raw_input.)
> (User can use upper or lower case, just lower it before test.)
> (I simplified the questions/answers so I could answer them.)
>
> I'm out of time but hope the following helps.
>
> Kind regards,
>
>
> Michael
>
>
> #!/usr/bin/env python
> """
> This is a game where you have to escape a dragon.
> By Saad Imran
>
> """
>
> import random
> import pygame
>
> # Define questions and answers.
>
> QUE = {1: "4 x 2",
> 2: "3 x 6",
> 3: "2 x 5",
> 4: "6 / 2",
> 5: "7 + 7",
> 6: "8 - 3",
> 7: "5 x 5",
> 8: "4 / 1",
> 9: "0 x 6", }
>
> ANS = {1: "8;75;75",
> 2: "18;75;75",
> 3: "10;75;75",
> 4: "3;75;75",
> 5: "14;75;75",
> 6: "5;75;75",
> 7: "25;75;75",
> 8: "4;75;75",
> 9: "0;75;75", }
>
> # Code to generate random number
> QUESEL = random.randrange(1, 10)
You can simplify that some more by putting question/answer pairs into a list
qa_pairs = [
("What is 4 x 2? ", "8"),
("What is 3 x 6? ", "18"),
#...
]
and then use random.choice() and tuple unpacking
question, answer = random.choice(qa_pairs)
if input(question) == answer: # python 2: replace input with raw_input
print("correct")
else:
print("UH-OH!")
You can nest the tuples if you really need other data
qa_pairs = [
("What is 4 x 2? ", ("8", 75, 75)),
("What is 3 x 6? ", ("18", 75, 75)),
#...
]
question, answer_and_offset = random.choice(qa_pairs)
answer, userx, dragonx = answer_and_offset
#...
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web