108 lines
4.5 KiB
Python
108 lines
4.5 KiB
Python
import random
|
|
|
|
class basics_007:
|
|
|
|
def rand_range(self):
|
|
"""Display a random integer between 1 and 100 inclusive."""
|
|
print(random.randrange(1,101))
|
|
|
|
def rand_choice(self):
|
|
"""Display a random fruit from a list of five fruits."""
|
|
fruits = ['Melón', 'Chirimoya', 'Plátano', 'Pera', 'Frutilla']
|
|
sel = random.choice(fruits)
|
|
print(sel)
|
|
|
|
def rand_game(self):
|
|
"""Randomly choose either heads or tails (\'h\' or \'t\'). Ask the user
|
|
to make their choice. If their choice is the same as the randomly
|
|
selected value, display the message \"You win\", otherwise display \"Bad
|
|
luck\". At the end, tell the user if the computer selected heads or tails."""
|
|
ops = {'h':'Cabeza', 't':'Cola'}
|
|
sel = random.choice(list(ops.keys()))
|
|
resp = input("Elige \'h\' (cabeza) o \'t\' (cola): ")
|
|
if resp == sel:
|
|
print(f"Ganaste, la opción era {sel} -> {ops.get(resp)}")
|
|
else:
|
|
print(f"Mala suerte, la opción era {sel} -> {ops.get(sel)}")
|
|
|
|
def rand_pick(self):
|
|
"""Randomly choose a number between 1 and 5. Ask the user to pick a
|
|
number. If they guess correctly, display the message \"Well done\",
|
|
otherwise tell them if they are too high or too low and ask them to pick
|
|
a second number. If they guess correctly on their second guess, display
|
|
\'Correct\', otherwise display \"You lose\"."""
|
|
num = random.randrange(1,6)
|
|
resp, cont = 0, 0
|
|
while resp != num and cont < 2:
|
|
resp = int(input(f"Ingresa un número (2 oportunidades, esta es la {cont+1}): "))
|
|
if resp > num:
|
|
print("Muy alto")
|
|
elif resp < num:
|
|
print("Muy bajo")
|
|
else:
|
|
print("🎉️ Correcto 🎊️")
|
|
break
|
|
cont += 1
|
|
if cont >= 2:
|
|
print("☠️ Perdiste 💀️")
|
|
|
|
def rand_10(self):
|
|
"""Randomly pick a whole number between 1 and 10. Ask the user to enter
|
|
a number and keep entering numbers until they enter the number that was
|
|
randomly picked."""
|
|
num = random.randrange(1,11)
|
|
resp = 0
|
|
while resp != num:
|
|
resp = int(input("Adivina el número (1-10): "))
|
|
|
|
def rand_10_upgrade(self):
|
|
"""Update program 056 so that it tells the user if they are too high or
|
|
too low before they pick again."""
|
|
num = random.randrange(1,11)
|
|
resp = 0
|
|
while resp != num:
|
|
resp = int(input("Adivina el número: "))
|
|
if resp > num:
|
|
print("Muy alto")
|
|
elif resp < num:
|
|
print("Muy bajo")
|
|
else:
|
|
print("🎉️ Correcto 🎊️")
|
|
|
|
def rand_quiz(self):
|
|
"""Make a maths quiz that asks five questions by randomly generating two
|
|
whole numbers to make the question (e.g. [num1] + [num2]). Ask the user
|
|
to enter the answer. If they get it right add a point to their score.
|
|
At the end of the quiz, tell them how many they got correct out of five."""
|
|
points = 0
|
|
for _ in range(5):
|
|
num_1 = random.randint(1,100)
|
|
num_2 = random.randint(1,100)
|
|
resp = int(input(f"Cuanto es {num_1} + {num_2}: "))
|
|
if resp == num_1+num_2:
|
|
points += 1
|
|
print(f"Puntaje: {points} de 5")
|
|
|
|
def rand_color(self):
|
|
"""Display five colours and ask the user to pick one. If they pick the
|
|
same as the program has chosen, say \"Well done\", otherwise display a
|
|
witty answer which involves the correct colour, e.g. \"I bet you are
|
|
GREEN with envy\" or \"You are probably feeling BLUE right now\". Ask
|
|
them to guess again; if they have still not got it right, keep giving
|
|
them the same clue and ask the user to enter a colour until they guess
|
|
it correctly."""
|
|
colors = {'rojo': 'Pienza con el corazón',
|
|
'azul': 'El cielo es...',
|
|
'verde': 'El color de la envidia es..',
|
|
'amarillo': 'El color de los patitos es..',
|
|
'blanco': 'La suma de todos los colores es..'}
|
|
rand_color = random.choice(list(colors.keys()))
|
|
resp = '0'
|
|
print(f"Estos son los colores: {', '.join(list(colors.keys()))}")
|
|
while resp != rand_color:
|
|
resp = input("Adivina el color: ").lower()
|
|
if resp == rand_color:
|
|
print("🎉️ Bien hecho 🎊️")
|
|
else:
|
|
print(f"Pista: {colors.get(rand_color)}")
|