4 Replies - 1850 Views - Last Post: 19 September 2012 - 12:47 PM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

I need help getting a certain part of my function to work

Posted 19 September 2012 - 11:49 AM

So what I am trying to do is get the averageTimeCounter to add the times of each repetition within the loop. Right now, when I run this program everything works fine, but averageTimeCounter only takes the value of the last time. I am trying to add up all the times each time the user decides to play, then create a variable called timesPlayed and divide averageTimeCounter by timesPlayed, so I can tell the user their average time. I have my fully documented code here.

Any Help will be Greatly Appreciated!

"""
Stephen Frazier
PA1
9/19/2012
The purpose is to pick a random word from a list, scramble it and get the user to guess it as fast as possible.
Then print out the users statistics
"""
from math import *
from random import *          #Imports all the needed modules for the assignment
from time import *
from scrambleFunction import scramble


def playGame():
    randList = ["computer", "science", "mouse", "keyboard", "mathematics", "memory", "programmer", "storage", "drive", "gigabyte"] #Creates a list of 10 words
    print("Welcome to the scramble game, I will present you with a word that has to do with computers, and you have to guess what it is!")
    sleep(6)
    print("\n")
    print("Ready, here we go!")
    sleep(2)
    print("\n")
    correctAnswers = 0 # Sets correctAnswers equal to 0
    wrongAnswers = 0 #Sets wrongAnswers equal to 0
    playAgain = "yes" #Starts playAgain at yes, so the loop will start
    while playAgain == "yes": #As long as playAgain is yes, then the loop will repeat
        RI = randint(0, len(randList)-1) #Assigns RI the value of one of the words in the list
        startTime = clock() #Starts the timing
        answer = input(scramble(randList[RI])) #Assigns answer, the value of what the user guesses
        averageTimeCounter = 0 #Sets the totalTimeCounter to 0
        if answer == randList[RI]: #Tells if what the user entered is the word at that position
            correctAnswers = correctAnswers+1 #Adds 1 to correctAnswers
            print("Good job, you got it!")
            endTime = clock() #Ends the timing
            totalTime = endTime - startTime #Calculates the total time
            print("It took you " ,totalTime, "seconds")
            if totalTime < 5: #If the user gets the answer in under 5 seconds, it tells them they're fast
                print("Wow, your fast!")
            elif 10>totalTime>5:
                print("Come on now, you do know where the keys are right?") #If they get it in more than 5 but less than 10, tells them that
            elif 15>totalTime>10:
                print("You know that you can use two hands to type, right?") #If they get it in more than 10 but less than 15, tells them that
            else:
                print("I can type faster than they with my feet!") #If they take more than 15 seconds, than it just tells them this
                print("\n")
            playAgain = input("Would you like to play again? Enter yes or no ") #Asks the user if they want to play again, and if they enter anything other than yes, it ends the loop
            if playAgain == "no":
                averageTimeCounter = averageTimeCounter+totalTime
                print("You got ", correctAnswers, "correct, and ", wrongAnswers, "incorrect, with an average time of ", averageTimeCounter) #Compares how many right and wrong the user got
                if correctAnswers > wrongAnswers:
                    print("Well you got more right than wrong, so Good Job! ") #If they got the more right than wrong, then it prints this
                elif  correctAnswers == wrongAnswers:
                    print("Well you broke even, same amount wrong as right.") #If they got the same amount wrong and right, it prints this
                else:
                    print("Well you have more wrong than you do right, Your a Failure! ") #If they got more wrong than right, it prints this
        else:
            wrongAnswers = wrongAnswers+1 #Adds 1 to wrongAnswers
            endTime2 = clock() #Creates a 2nd time ender, in case they do not get it right
            totalTime2 = endTime2 - startTime #Calculates their total time
            print("Congratulations it took you ", totalTime2, " seconds to get the wrong answer")
            playAgain = input("The word was " + randList[RI] + " do you want to try another word? Enter yes or no ") #Assigns whatever the user enters as playAgain, to either end of keep the loop going
            if playAgain == "no":
                print("You got ", correctAnswers, "correct, and ", wrongAnswers, "incorrect")
                if correctAnswers > wrongAnswers:
                    print("Well you got more right than wrong, so Good Job! ")#If they got the more right than wrong, then it prints this
                elif  correctAnswers == wrongAnswers:
                    print("Well you broke even, same amount wrong as right.") #If they got the same amount wrong and right, it prints this
                else:
                    print("Well you have more wrong than you do right, Your a Failure! ") #If they got more wrong than right, it prints this




playGame()




Is This A Good Question/Topic? 0
  • +

Replies To: I need help getting a certain part of my function to work

#2 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: I need help getting a certain part of my function to work

Posted 19 September 2012 - 12:27 PM

Nevermind, I figured it out myself
Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: I need help getting a certain part of my function to work

Posted 19 September 2012 - 12:28 PM

Care to share your solution?
Was This Post Helpful? 0
  • +
  • -

#4 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: I need help getting a certain part of my function to work

Posted 19 September 2012 - 12:46 PM

Sure,

I put averageTimeCounter = 0 inside the loop. So every time it restarted it restarted averageTimeCounter back at 0. So I just had to move the assignment statement outside of the loop, and it worked fine.
Was This Post Helpful? 1
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: I need help getting a certain part of my function to work

Posted 19 September 2012 - 12:47 PM

Great, glad it's working. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1