Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guessing games
#1
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020