Saltar al contenido

ejemplo de código python del juego de adivinanzas de números

Contamos con el hallazgo a este enigma, o por lo menos eso pensamos. Si tienes dudas coméntalo, que sin dudas

Ejemplo 1: juego de adivinanzas 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")

Ejemplo 2: juego de adivinanzas en 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

Ejemplo 3: como adivinar el número en python

import random
 
hidden = random.randrange(1, 201)
print(hidden)
 
guess = int(input("Please enter your guess: "))
 
if guess == hidden:
    print("Hit!")
elif guess < hidden:
    print("Your guess is too low")
else:
    print("Your guess is too high")

Ejemplo 4: juego de adivinanzas de números 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()

Reseñas y calificaciones del post

Si haces scroll puedes encontrar las explicaciones de otros programadores, tú igualmente eres capaz mostrar el tuyo si lo crees conveniente.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *