number guessing game that adds points for every loop

Question:

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!")
Asked By: sidewaysblue

||

Answers:

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!")
Answered By: Kushaal Mahajan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.