Python Forum

Full Version: guess my number GAME
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello everyone... Smile

here is a simple game of guessing a number from 0 to 100
you can guess computer number or have computer guess your number...
i didn't do any input validations cause... i'm lazy?

any way here is the code:

import os
import random
import time

width = os.get_terminal_size().columns
# turns = 10


def clear(): return os.system('cls')


def opening():
    turns = 10
    print("GUESS MY NUMBER GAME".center(width))
    print()
    print("PRESS 1. TO GUESS COMPUTER NUMBER (BETWEEN 0-100) OR 2. TO HAVE THE COMPUTER GUESS YOUR NUMBER BETWEEN (0-100)".center(width))
    print()
    option1 = int(input("> "))
    clear()
    if option1 == 1:
        comp_num(turns)
    elif option1 == 2:
        human_num(turns)


def comp_num(turns):
    print("GUESS THE COMPUTER NUMBER... PRESS ANY KEY TO COMTINUE...".center(width))
    a = input()
    num1 = random.randint(0, 100)
    in_game = True
    while in_game == True:
        print(
            f"YOU HAVE {turns} TO GUESS THE NUMBER OF THE COMPUTER THAT IS BETWEEN 0-100".center(width))
        guess1 = input("> ")
        if int(guess1) < num1:
            print(
                f"COMPUTER NUMBER IS LARGER THEN {guess1}")
        elif int(guess1) > num1:
            print(
                f"COMPUTER NUMBER IS SMALLER THEN {guess1}")
        elif int(guess1) == num1:
            print("CORRECT! YOU GUESSED THE NUMBER!")
            in_game = False
        # a = input("press any key to continue to next guess")
        turns -= 1
        # clear()
        if turns <= 0:
            print("TEN TURNS PASSED GAME OVER")
            in_game = False


def human_num(turns):
    low = 0
    high = 100
    in_game2 = True
    clear()
    print("CHOOSE A NUMBER BETWEEN 0 AND 100".center(width))
    time.sleep(4)
    clear()
    while in_game2 == True:
        print(f"COMPUTER HAD {turns} LEFT TO GUESS YOUR NUMBER".center(width))

        guess2 = int((high + low) / 2)

        print(f"IS THE NUMBER {guess2} ???")
        print("PRESS 1 KEY IF COMPUTER GUESS IS HIGHER THEN YOUR NUMBER")
        print("PRESS 2 KEY IF COMPUTER GUESS IS LOWER THEN YOUR NUMBER")
        print("PRESS 3 KEY IF COMPUTER GUESS IS CORRECT")
        option2 = int(input("> "))

        if option2 == 1:
            high = guess2
        elif option2 == 2:
            low = guess2
        elif option2 == 3:
            print("COMPUTER SUCCEDED IN GUESSING YOUR NUMBER!")
            in_game2 = False
        turns -= 1
        if turns <= 0:
            print("COMPUTER RAN OUT OF TURNS! GAME OVER")
            in_game2 = False


def main():
    # turns = 10
    while True:
        opening()
        option3 = input("PLAY AGAIN? (y/n): ")
        if option3.lower() == "n":
            break
    # elif option1 == 2:
    #     human_num(turns)


main()
It would be cool if it adjusted the range as you guessed.

Output:
14 YOU HAVE 5 TO GUESS THE NUMBER OF THE COMPUTER THAT IS BETWEEN 15-100
Hello,

I just ran your code and tried your program, very cool! I hope I am one of the first happy users of your game!

This is awesome man, keep it up! It feels awesome to complete a program doesn't it? One thing that keeps me at it, it feels so good to accomplish!

Keep it up!