0

The project is to write a game that has a randomly generated number and you need to guess the number using various functions and return statements. Whenever you try this, this code asks for your guess, then doesnt return anything.

from random import randint 

TotalPoints = 0


def generateGuess():
    return(randint(1,10))

def CheckGuess(playerGuess, robotGuess, correct): 

    correct = False

    if playerGuess == robotGuess:
        global TotalPoints
        TotalPoints = TotalPoints + 5
        correct = True
        return correct
    else:
        correct = False
    return correct 


def retry ():    
    playagain = int(input("Do you want to play again? 1 for yes, 2 for no: "))

    if (playagain == 1):
        main()
    elif (playagain == 2):
        print("Game over! You ended with ", TotalPoints, "points!")
    else:
        print("error")


def main(): 
    print("Let's play the number Guessing Game! ")    

    playerGuess = int(input("Make a guess: "))
    robotGuess = generateGuess
    
    CheckGuess(playerGuess, robotGuess)


    if CheckGuess (correct = True):
        print("You guessed right! You have ", TotalPoints, "points!")
    elif CheckGuess (correct = False):
        print("You lost this round!")



main()
Samwise
  • 68,105
  • 3
  • 30
  • 44
Arthur
  • 13
  • 1
  • 1
    You need to call `generateGuess()` as a function (with the parenthesis). Otherwise you are just creating an alias to the function and not calling it. You should also be storing the result of `CheckGuess`, such as `result = CheckGuess(playerGuess, robotGuess)`. Then you can do `if result:` to see if the user guessed correctly. – h0r53 Jul 06 '22 at 19:29
  • 1
    And you call `CheckGuess` with single `correct` arguments while there are 3 required. – STerliakov Jul 06 '22 at 19:30
  • 1
    You could simplify your `CheckGuess` by removing the third argument. It is pointless. – h0r53 Jul 06 '22 at 19:30
  • 2
    When I run this code, I get an error, rather than it printing nothing. Did you get the same error? If so, the first step in debugging is to *read the error message* rather than treating it as "nothing" -- error messages are there to help you debug! – Samwise Jul 06 '22 at 19:31
  • See the "Tricks for Trimming" section of http://sscce.org/ for guidance on narrowing your problem; to be on-topic here (or in most other forums), we expect some work to be done to build a [mre] -- the _shortest possible_ code that generates a well-defined problem when run without changes. – Charles Duffy Jul 06 '22 at 19:32
  • `robotGuess = generateGuess` You forgot the parentheses `()` after the function call... – John Gordon Jul 06 '22 at 19:34
  • Welcome to Stack Overflow! Please take the [tour] and read [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341) as well as [ask], which has tips like how to write a good title. If you want debugging tools, check out [How to step through Python code to help debug issues?](/q/4929251/4518341) Most of them are a bit much for a newbie, but [I posted an answer about Python Tutor](/a/57532523/4518341), which you might like. – wjandrea Jul 06 '22 at 19:45

1 Answers1

1

You have several errors in your code as mentioned in the comments. Here is a corrected, functional version that simplifies some of your logic and achieves the behavior you seem to be going for.

from random import randint

total_points = 0

def generate_guess():
    return randint(1,10)

def check_guess(player_guess, robot_guess):
    global total_points
    correct = False

    if player_guess == robot_guess:
        total_points += 5
        correct = True

    return correct


def retry():
    play_again = int(input("Do you want to play again? 1 for yes, 2 for no: "))

    if play_again == 1:
        main()
    elif play_again == 2:
        print(f"Game over! You ended with {total_points} points!")
    else:
        print("error")


def main():
    print("Let's play the number Guessing Game! ")

    player_guess = int(input("Make a guess: "))
    robot_guess = generate_guess()

    result = check_guess(player_guess, robot_guess)

    if result:
        print(f"You guessed right! You have {total_points} points!")
    else:
        print("You lost this round!")

    retry()

main()

The main issues were syntactic.

  • Not using parenthesis when calling functions
  • Not storing function results
  • Issues with passing arguments to functions and returning values
h0r53
  • 3,034
  • 2
  • 16
  • 25
  • It'd be nice to fix basic formatting (`retry ()`, f-strings, `global` position, `+=` ignorance, `return(...)`) and naming (`CamelCase` and js-like function names) issues in this code - it is a good way to show beginners existing common conventions and recommend following them. – STerliakov Jul 06 '22 at 19:40
  • 1
    @SUTerliakov and Arthur I've updated the answer to reflect more common python conventions for variable names, function names, syntax, and usage of programming constructs – h0r53 Jul 06 '22 at 19:51