mercredi 17 avril 2019

Random number guessing game with menu

I have the code created to with a menu to choose either to guess or number or have the cpu guess a number.

**#display Guessing Game menu**
def DisplayGameMenu():
    print()
    print("1. You guess the number")
    print("2. You type a number and see if the computer can guess it")
    print("3. Exit")
    gameMenu = int(input("What is your choice:  "))
    while gameMenu not in [1, 2, 3]:
        print("Not a valid choice")
        gameMenu = input("What is your choice:  ")
    return gameMenu

**#User Input for Random Number**
def myInput(userGuessTaken):
    for i in range(userGuessTaken):
        return userGuessTaken + 1

**#CPU Input for Random Number**    
def cpuInput(cpuGuessTaken):
    for i in range(cpuGuessTaken):
        return cpuGuessTaken + 1

**#User Guess**
def userChoice():
    while True:
        myGuess = input()
        myGuess = int(myGuess)

        if myGuess < myNumber:
            print("Too low.")                          
        if myGuess > myNumber:
            print("Too high.")
        if myGuess == myNumber:
            break

        if myGuess == myNumber:
            userGuessTaken = str(userGuessTaken)
            print("You guessed it! It took you "+ str(userGuessTaken) + " attempts.")

    if myGuess != myNumber:
        mynumber = str(myNumber)
        print("Sorry the number was " + myNumber)

**#CPU Guess**
def cpuChoice():
    while True:
        cpuGuess = input()
        cpuGuess = int(cpuGuess)

        cpuGuessTaken = cpuGuessTaken + 1

        if cpuGuess < cpuNumber:
            print("The computer guessed" + str(cpuGuess) + "which is too low")                          
        if cpuGuess > cpuNumber:
            print("The computer guessed" + str(cpuGuess) + "which is too high") 
        if cpuGuess == cpuNumber:
            break

        if cpuGuess == cpuNumber:
            cpuGuessTaken = str(cpuGuessTaken)
            print("The computer guessed it! It took" + str(cpuGuessTaken) + " attempts.")

        if cpuGuess != cpuNumber:
            mynumber = str(cpuNumber)
            print("Sorry the number was " + cpuNumber)


**#main program**
def main():
    userGuessTaken = 0
    cpuGuessTaken = 0
    userGuessTaken = myInput(userGuessTaken)
    cpuGuessTaken = cpuInput(cpuGuessTaken) 

    print("Welcome to my Guess the number program!")
    print()
    print(today)
    print()
    print("What is your name? ")
    userName = input()
    print("Welcome to my Guess the number program! "  + userName)
    print()

    **#Guessing Game menu Choices**
    gameMenu =""
    while gameMenu != 3:
       gameMenu = DisplayGameMenu()
        if gameMenu == 1:
            userChoice(userGuessTaken)
            print("Guess a number between 1 and 10:  ")
        elif gameMenu == 2:
            cpuChoice(cpuGuessTaken)  
            print("Please enter a number between 1 and 10 for the computer to guess:")
        else:
            print("Thank you for playing the guess the number game!")


if __name__== "__main__":
    main()

Welcome to my Guess the number program!

You guess the number You type a number and see if the computer can guess it Exit What is your choice: 1

Please guess a number between 1 and 10: 8

Too high

Please guess a number between 1 and 10: 7

You guessed it! It took you 2 attempts

You guess the number You type a number and see if the computer can guess it Exit What is your choice: 2

Please enter a number between 1 and 10 for the computer to guess: 5

The computer guessed 7 which is too high

The computer guessed 2 which is too low

The computer guessed 9 which is too high

The computer guessed 8 which is too high

The computer guessed 8 which is too high

The computer guessed 1 which is too low

The computer guessed 3 which is too low

The computer guessed 4 which is too low

The computer guessed 2 which is too low

The computer guessed 8 which is too high

The computer guessed 3 which is too low

The computer guessed 1 which is too low

The computer guessed 4 which is too low

The computer guessed 7 which is too high

The computer guessed 1 which is too low

The computer guessed 7 which is too high

The computer guessed 1 which is too low

The computer guessed 4 which is too low

The computer guessed 4 which is too low

The computer guessed it! It took 20 attempts

You guess the number You type a number and see if the computer can guess it Exit What is your choice: 3

Thank you for playing the guess the number game!




Aucun commentaire:

Enregistrer un commentaire