lundi 13 mai 2019

Program that keeps running until user types a word

Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (_Hint: remember to use the user input lessons from the very first exercise

Extras:

Keep the game going until the user types “exit” Keep track of how many guesses the user has taken, and when the game ends, print this out.

I have divided my program in three functions

import random

# function to generate random number between 1 and 9
def generate():
    cpu_number=random.randint(1,9)
    print(cpu_number)

# function to input player guess
def player_guess():
    player_number=input('Digit a number between 1 and 9\n')

#function to keep asking the player the value unless he writes exit. Also return wrong and right guesses
def game():
    right_guesses=0
    wrong_guesses=0
    while player_guess()!= 'exit':
        if player_guess() > generate():
            print('Higher value than the value generated!')
            wrong_guesses += 1
        elif player_guess() < generate():
            print('Lower value than the value generated!')
            wrong_guesses += 1
        else:
            print('You have guessed the correct value!!!!')
            right_guesses += 1
    print('You have entered',right_guesses,'guesses and',wrong_guesses,'guesses')
    return right_guesses,wrong_guesses

print(generate())
print(player_guess())
print(game())

My program is not running the if sentences. Also it does not stop if i type exit

Aucun commentaire:

Enregistrer un commentaire