Python Forum

Full Version: Guessing games
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I made two types of guessing games-
1. User guesses a random number
# 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.
import random
the_number = random.randint(0,9)
user_guess = 0
number_of_guesses = 1
while user_guess != the_number :
    user_guess = int(input("Please guess your number between 0 and 9, including both of them : ")) 
    if user_guess == the_number:
        print("You guessed it correct!")
        if number_of_guesses == 1:
            print("You took " + str(number_of_guesses) + " guess") 
        else :
            print("You took " + str(number_of_guesses) + " guesses")
    elif user_guess > the_number :
        print("Too high !")
        number_of_guesses += 1
    else : 
        print("Too low !")
        number_of_guesses += 1
2. This time, computer guesses number user has in mind
# You, the user, will have in your head a number between 0 and 100. The program will guess a number, and you, the user, will say whether it is too high, too low, or your number.
import random
print("Let's play a game!")
print("You, the user will try to think of a number and I, the program, will try to guess it!")
num1 = random.randint(0, 10)
count = 1
play = True
while play:
    print ("Is it " + str(num1) + "?")
    ans = input()
    if ans.lower() == "no":
        print ("Higher or lower?")
        ans = input()
        print("Number of guesses made: " + str(count))
        if ans.lower() == "higher":
            num1 += random.randint(1, 4)
        elif ans.lower() == "lower":
            num1 -= random.randint(1, 4)
    elif ans.lower() == "yes":
        print("I got it in " + str(count) + " try/tries!")
        play = False
    count += 1