lundi 24 octobre 2016

Guessing Number Game with Python

I am writing a program that plays a guessing game with a user. The game starts with the computer choosing a random integer between 0 and 100 (inclusive). The program then continually prompts the user to guess the value until the user guesses the secret number (or enters -1 to give up). If the user enters -1, the user is giving up therefore the program prints out the number and the amount of guesses (not including -1). Currently my program is not running and I can't seem to figure out why.

from random import randint guessesTaken = 0

def computer_guess(randomNumber): low = 1 high = 100 giveUp = -1

randomNumber = randint(1,100)
while UserGuess != randomNumber:
    print('Take a guess' + randomNumber)
    UserGuess = input()
    UserGuess = int(UserGuess)

    guessesTaken += 1

    if UserGuess < randomNumber:
        print("Your guess is too low")
    if UserGuess > randomNumber:
        print("Your guess is too low")
    if UserGuess == randomNumber or UserGuess == giveUp:
        break

if UserGuess == randomNumber:
        guessesTaken = str(guessesTaken)
        print("Yes, that is right!")
        print("It took you " + guessesTaken + " guesses")
else:
        guessesTaken = str(guessesTaken)
        print("Better luck next time.")
        print("You tried " + guessesTaken + " guesses")

print (randomNumber)

def main():
randomNumber = int(input("Enter a number: "))
if randomNumber < 1 or randomNumber > 100:
    print("Must be in range [1, 100]")
else:
    computer_guess(randomNumber)

if __name__ == '__main__':
main()




Aucun commentaire:

Enregistrer un commentaire