Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

number guessing game that adds points for every loop

i’m trying to write a random number guessing game with a specific structure. if user gets the number wrong or right, they need to have the option to try again. if they’re right, then 5 points are added to the TotalPoints variable. here’s the code


from random import randint

#generates a random guess
def GenerateGuess():
      return (randint(1, 10))

TotalPoints = 0


UserGuess = int(input("Enter any number: "))

#takes users guess and random int, and adds points to TotalPoints if correct
#return True if correct, return False if userguess doesnt match random int
def CheckGuess():
    if UserGuess == GenerateGuess:
        print("True")
        TotalPoints == TotalPoints + 5
    else:
        print("False")


def main():
    print("Let's play the Number Guessing Game!")
    UserGuess = int(input("Make a guess: "))
    CheckGuess(UserGuess)


    PlayAgain = input("Play Again? Type 'Yes' or 'No': ")
    if PlayAgain == "Yes":
        main()
    if PlayAgain == "No":
        No =  print("Goodbye!")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Incorrect Code

from random import randint

#generates a random guess
def GenerateGuess():
      return (randint(1, 10))

TotalPoints = 0


UserGuess = int(input("Enter any number: "))

#takes users guess and random int, and adds points to TotalPoints if correct
#return True if correct, return False if userguess doesnt match random int
def CheckGuess():
    if UserGuess == GenerateGuess:
        print("True")
        TotalPoints == TotalPoints + 5
    else:
        print("False")


def main():
    print("Let's play the Number Guessing Game!")
    UserGuess = int(input("Make a guess: "))
    CheckGuess(UserGuess)


    PlayAgain = input("Play Again? Type 'Yes' or 'No': ")
    if PlayAgain == "Yes":
        main()
    if PlayAgain == "No":
        No =  print("Goodbye!")

Problems
Problem1-In function check guess you haven’t put () that’s it was not comparing the guesses properly
Problem2-You Have put double equal to (=) sign when adding points

Corrected Code

from random import randint

#generates a random guess
def GenerateGuess():
      return (randint(1, 10))

TotalPoints = 0

#takes users guess and random int, and adds points to TotalPoints if correct
#return True if correct, return False if userguess doesnt match random int
def CheckGuess(UserGuess):
    if UserGuess == GenerateGuess():
        print("True")
        TotalPoints = TotalPoints + 5
    else:
        print("False")


def main():
    print("Let's play the Number Guessing Game!")
    UserGuess = int(input("Make a guess: "))
    CheckGuess(UserGuess)


    PlayAgain = input("Play Again? Type 'Yes' or 'No': ")
    if PlayAgain == "Yes":
        main()
    if PlayAgain == "No":
        No =  print("Goodbye!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading