Please help with this

mkharper mkharper at gmail.com
Wed Nov 13 10:02:22 CET 2013


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



More information about the Python-list mailing list