Python Forum
guess my number GAME
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
guess my number GAME
#1
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()
Reply
#2
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
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter number guessing game menator01 2 3,396 Mar-24-2022, 07:13 PM
Last Post: menator01
  Guess your number joe_momma 0 2,418 Oct-23-2020, 02:53 AM
Last Post: joe_momma
  Guess my number foksikrasa 0 2,364 May-28-2020, 04:12 PM
Last Post: foksikrasa
  Mastermind/Guess the Code Game JoeLamond 5 12,193 Jan-14-2019, 06:34 PM
Last Post: steve_shambles
  Guess the dice roll mini-game tawnnx 6 7,248 May-22-2018, 02:12 PM
Last Post: malonn

Forum Jump:

User Panel Messages

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