Pick a number

What to Do

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

——————————————————–

IMPORT

Primeramente necesitamos generar un número aleatorio, ya existen librerías en python para generar aleatorios, lo que haría con la palabra “import” sería eso, importarlos desde la librería , ya solo faltaría saber qué se va a importar, buscar el random.

“… la función randint de este módulo toma dos argumentos y devuelve un número entero al azar entre ellos (incluidos).”

bien, eso nos sirve.

y como a todo; le pondremos un nombre a esa variable que será el número aleatorio.

import random
x=random.randint(1, 100)

——————————————————–

WHILE

en python no se usan las llaves “{}”

simplemente se pone “while (condicion):”

Q

elif

——————————————————–

 para nuestro querido ciclo se necesitará saber sobre los acumuladores y contadores.

Contador: variable cuyo valor incrementa o decrementa en una cantidad constante CADA repetición.

Acumulador: Almacena cantidades resultantes de operaciones sucesivas.

La diferencia entre estos es que el incremento o decremento del acumulador es variable y no constante

——————————————————–

print(“E e e e eso  es to to to do a

mi gos “)

Código

import random
x=random.randint(1, 100)
print(“Este programa generará un número aleatorio entre el 1 y 100 con el fin de que usted lo adivine”)
num=0
veces=0

while num != x:
veces+=1
num = int(input(“Adivina el numero: “))
if (num>x):
print(“muy alto”)
elif (num<x):
print(“muy bajo”)

print(“El número es correcto, “+(str(x)+”t”))
print(“Intentos: ” + (str(veces)+”t”));

 

CC BY-SA 4.0 Pick a number by MajoDavila is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.