Answers for "number guessing game python"

7

guessing game python

# updated version
import random
# for instructions so that the user understands
def instructions():
    print("Welcome to the guessing game you will have 3 tries to pick a number 1-10")
    print("Good luck btw it's all random")


instructions()
# guess limit so the user can't guess too much.
guess_limit = 1
# The random guess
number = random.randint(1, 10)
# What users can type and see.
user = int(input("What is the number?: "))
# The while loop so it can go on.
while user != number:

    if user > number:
        print("Lower")

    elif user < number:
        print("Higher")

    user = int(input("What is the number?: "))
    guess_limit += 1
    if guess_limit == 3:
        print("------------------------------------------------------")
        print("You ran out of guess ;( the answer was", number, "<--")
        print("------------------------------------------------------")
        break
else:
    print("You guessed it right! The number is", number,
          "and it only took you ", guess_limit, "tries")
Posted by: Guest on October-22-2020
4

random number guessing game python

import random
print("Welcome to random number gusser game")
print("I will guss number from 1 to 100 ")
Difficulty = str(input("What is your difficulty Easy or hard:- ")).lower()
def difficiulty(Difficulty):
    random_number = random.randint(1,100)
    if Difficulty == "easy":
        print("you will get 10 chances")
        chances = 10
        while True:
            number_choosen = int(input("Guss the number now:- "))
            if number_choosen != random_number:
                chances -= 1
                if chances == 0:
                    return "You Lost"
                    break
                else:
                    print("Not correct number")
            else:
                return "you won"
    elif Difficulty == "hard":
        chances = 5
        print("You will get 5 chances")
        while True:
            number_choosen = int(input("Guss the number now:- "))
            if number_choosen != random_number:
                chances -= 1
                if chances == 0:
                    return "You Lost"
            else:
                return "You Won"
print(difficiulty(Difficulty))
Posted by: Guest on June-30-2021
3

guessing game in python

# importing random library
# this game untill work untill u get  right answer
import random
a = 1
rand = random.randint(1, 100)
while 1 <= a:
    a += 1
    winin = int(input("enter a number under 100 : "))
    if winin > 100:
        print("not valid")
        break
    else:
        if winin < rand:
            print("too low")
        elif winin > rand:
            print("too high")
        if winin == rand:
            print(f"you got the right answer in {a - 1} times")
            break
Posted by: Guest on April-11-2021
0

number guessing game python gui

# guess the number in a GUI

from tkinter import *
import random


class Application(Frame):
    ''' GUI guess the number application  '''
    def __init__(self, master):
        '''initialise frame'''
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()


    def create_widgets(self):
        '''create widgets for GUI guess game'''
        #Create title label
        Label(self, text = 'Guess the number'
              ).grid(row = 0, column = 1, columnspan = 2, sticky = N)
        # create instruction labels
        Label(self, text = 'I am thinking of a number between 1 and 100'
              ).grid(row = 1, column = 0, columnspan = 3, sticky = W)

        Label(self, text = 'Try to guess in as few attempts as possible'
              ).grid(row = 2, column = 0, columnspan = 3, sticky = W)

        Label(self, text = 'I will tell you to go higher or lower after each guess'
              ).grid(row = 3, column = 0, columnspan = 3, sticky = W)
        # Create a label and number entry
        Label(self, text = 'Your guess: '
              ).grid(row = 4, column = 0, sticky = W)
        self.guess_ent = Entry(self)
        self.guess_ent.grid(row = 4, column = 1, sticky = W)
        # create label and text box for number of tries
        Label(self, text = ' number of tries: '
              ).grid(row = 5, column = 0, sticky = W)
        self.no_tries_txt = Text(self, width = 2, height = 1, wrap = NONE)
        self.no_tries_txt.grid(row = 5, column = 1, sticky = W)

        # create guess button

        Button(self, text = 'Guess', command = self.check_if_correct
               ).grid(row = 5, column = 2, sticky = W)

        self.result_txt = Text(self, width = 80, height = 15, wrap = WORD)
        self.result_txt.grid(row = 6, column = 0, columnspan = 4)

        # display the results
        #self.result_txt.delete(0.0, END)
        #self.result_txt.insert(0.0, display_result)
        #self.no_tries_txt.delete(0.0, END)
        #self.no_tries_txt.insert(0.0, the_number)

    def rnumber(self):
        self.rnumber = random.randint(1, 100)


    def check_if_correct(self):

        self.result = ""
        gnum = self.guess_ent.get()
        gnum = int(gnum)

        if gnum == self.rnumber:
            gnum = str(gnum)
            self.result = gnum + " is the magic number!\n"
        elif gnum < self.rnumber:
            gnum = str(gnum)
            self.result = gnum + " is too low.\n"
        elif gnum > self.rnumber:
            gnum = str(gnum)
            self.result= gnum + " is too high.\n"


    def giveResult(self):
        return str(self.result)


        # display the results
        self.result_txt.delete(0.0, END)
        self.result_txt.insert(0.0, result)
        #self.no_tries_txt.delete(0.0, END)
        #self.no_tries_txt.insert(0.0, the_number)


# main
root = Tk()
root.title('Guess the Number')
app = Application(root)
root.mainloop()
Posted by: Guest on August-05-2020

Code answers related to "number guessing game python"

Python Answers by Framework

Browse Popular Code Answers by Language