+: basicos 052-059
This commit is contained in:
parent
5570edbd48
commit
c87350d5ae
@ -4,6 +4,7 @@ from . import basic03 as ex03
|
||||
from . import basic04 as ex04
|
||||
from . import basic05 as ex05
|
||||
from . import basic06 as ex06
|
||||
from . import basic07 as ex07
|
||||
from common.common import (
|
||||
print_challenges,
|
||||
user_input,
|
||||
@ -234,3 +235,33 @@ def challenges06():
|
||||
exit(0)
|
||||
case _:
|
||||
continue
|
||||
|
||||
def challenges07():
|
||||
select_ok = False
|
||||
while not select_ok:
|
||||
clear()
|
||||
i, ex7 = print_challenges(ex07.basics_007)
|
||||
selection = user_input(i)
|
||||
match selection:
|
||||
case 1:
|
||||
run_func(ex7.rand_range)
|
||||
case 2:
|
||||
run_func(ex7.rand_choice)
|
||||
case 3:
|
||||
run_func(ex7.rand_game)
|
||||
case 4:
|
||||
run_func(ex7.rand_pick)
|
||||
case 5:
|
||||
run_func(ex7.rand_10)
|
||||
case 6:
|
||||
run_func(ex7.rand_10_upgrade)
|
||||
case 7:
|
||||
run_func(ex7.rand_quiz)
|
||||
case 8:
|
||||
run_func(ex7.rand_color)
|
||||
case 'v':
|
||||
return
|
||||
case 's':
|
||||
exit(0)
|
||||
case _:
|
||||
continue
|
||||
|
107
basic/basic07.py
Normal file
107
basic/basic07.py
Normal file
@ -0,0 +1,107 @@
|
||||
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)}")
|
4
main.py
4
main.py
@ -29,7 +29,7 @@ def basic_challenges():
|
||||
clear()
|
||||
print(content)
|
||||
opcs_default()
|
||||
selection = user_input(6)
|
||||
selection = user_input(7)
|
||||
match selection:
|
||||
case 1:
|
||||
basic.challenges01()
|
||||
@ -44,7 +44,7 @@ def basic_challenges():
|
||||
case 6:
|
||||
basic.challenges06()
|
||||
case 7:
|
||||
pass
|
||||
basic.challenges07()
|
||||
case 'v':
|
||||
return
|
||||
case 's':
|
||||
|
Loading…
Reference in New Issue
Block a user