From 5570edbd48c144a797e7c21166a97d0af9b6057b Mon Sep 17 00:00:00 2001 From: devfzn Date: Fri, 10 Nov 2023 01:57:44 -0300 Subject: [PATCH] +: basicos 045-051 --- basic/basic.py | 30 ++++++++++++++ basic/basic06.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++ main.py | 4 +- 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 basic/basic06.py diff --git a/basic/basic.py b/basic/basic.py index c8eb9d9..9606a1f 100644 --- a/basic/basic.py +++ b/basic/basic.py @@ -3,6 +3,7 @@ from . import basic02 as ex02 from . import basic03 as ex03 from . import basic04 as ex04 from . import basic05 as ex05 +from . import basic06 as ex06 from common.common import ( print_challenges, user_input, @@ -28,6 +29,7 @@ def challenges_sublist(): case 5: challenges05() case 6: + challenges06() pass case 7: pass @@ -204,3 +206,31 @@ def challenges05(): exit(0) case _: continue + +def challenges06(): + select_ok = False + while not select_ok: + clear() + i, ex6 = print_challenges(ex06.basics_006) + selection = user_input(i) + match selection: + case 1: + run_func(ex6.over50) + case 2: + run_func(ex6.last_5) + case 3: + run_func(ex6.add_another) + case 4: + run_func(ex6.party) + case 5: + run_func(ex6.compnum) + case 6: + run_func(ex6.between) + case 7: + run_func(ex6.bottles) + case 'v': + return + case 's': + exit(0) + case _: + continue diff --git a/basic/basic06.py b/basic/basic06.py new file mode 100644 index 0000000..2f69a86 --- /dev/null +++ b/basic/basic06.py @@ -0,0 +1,104 @@ +class basics_006: + + def over50(self): + """Set the total to 0 to start with. While the total is 50 or less, ask + the user to input a number. Add that number to the total and print the + message \"The total is… [total]\". Stop the loop when the total is over + 50""" + total = 0 + while total <= 50: + num = int(input("Ingresa un número: ")) + total += num + print(f"El total es {total}") + + def last_5(self): + """Ask the user to enter a number. Keep asking until they enter a value + over 5 and then display the message \"The last number you entered was a + [number]\" and stop the program.""" + num = 0 + while num <= 5: + num = int(input("Ingresa un número: ")) + print(f"El último número ingresado fue [{num}]") + + def add_another(self): + """Ask the user to enter a number and then enter another number. Add + these two numbers together and then ask if they want to add another + number. If they enter \"y\", ask them to enter another number and keep + adding numbers until they do not answer \"y\". Once the loop has + stopped, display the total.""" + resp = 's' + num_1 = int(input("Ingresa un número: ")) + while resp == 's': + num_2 = int(input("Ingresa otro número: ")) + num_1 += num_2 + resp = input("Deseas agregar otro número? (s|n): ").lower() + print(f"El total es {num_1}") + + def party(self): + """Ask for the name of somebody the user wants to invite to a party. + After this, display the message \"[name] has now been invited\" and add + 1 to the count. Then ask if they want to invite somebody else. Keep + repeating this until they no longer want to invite anyone else to the + party and then display how many people they have coming to the party.""" + cont = 's' + guests = 0 + while cont == 's': + name = input("Ingresa el nombre de quien quieras invitar: ") + print(f"{name} ha sido invitado") + guests += 1 + cont = input("Deseas invitar a otra persona? (s|n): ") + print(f"Has invitado a {guests} persona(s)") + + def compnum(self): + """Create a variable called compnum and set the value to 50. Ask the + user to enter a number. While their guess is not the same as the compnum + value, tell them if their guess is too low or too high and ask them to + have another guess. If they enter the same value as compnum, display + the message \"Well done, you took [count] attempts\".""" + compnum = 50 + count = 0 + guess = 0 + while guess != compnum: + guess = int(input("Ingresa un número: ")) + count += 1 + if guess < compnum: + print("Muy bajo") + elif guess > compnum: + print("Muy alto") + print(f"Bien hecho, solo te tomó {count} intentos") + + def between(self): + """Ask the user to enter a number between 10 and 20. If they enter a + value under 10, display the message \"Too low\" and ask them to try + again. If they enter a value above 20, display the message \"Too high\" + and ask them to try again. Keep repeating this until they enter a value + that is between 10 and 20 and then display the message \"Thank you\".""" + num = int(input("Ingresa un número entre 10 y 20: ")) + while (num < 10 or num > 20): + if num < 10: + print("Muy Bajo") + elif num > 20: + print("Muy Alto") + num = int(input("Intenta nuevamente: ")) + print("Gracias") + + def bottles(self): + """Using the song \"5 green bottles\", display the lines \"There are + [num] green bottles hanging on the wall, [num] green bottles hanging on + the wall, and if 1 green bottle should accidentally fall\". Then ask the + question \"how many green bottles will be hanging on the wall?\" If the + user answers correctly, display the message \"There will be [num] green + bottles hanging on the wall\". If they answer incorrectly, display the + message \"No, try again\" until they get it right. When the number of + green bottles gets down to 0, display the message \"There are no more + green bottles hanging on the wall\".""" + action = [ 'al cerro', 'de viejuno', 'de tos', 'en tren', 'para el campo'] + num = 5 + while num > 0: + print(f"Yo tenia {num} perritos! Yo tenia {num} perritos!") + num -= 1 + print(f"Uno se fue {action[num]}, no me quedan mas que \'?\'") + resp = int(input("¿Cuantos perritos quedan?: ")) + while resp != num: + resp = int(input("Intenta nuevamente: ")) + print(f"Uno se fue {action[num]}, no me quedan mas que {resp}") diff --git a/main.py b/main.py index 30b927f..bf3552e 100755 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ def basic_challenges(): clear() print(content) opcs_default() - selection = user_input(5) + selection = user_input(6) match selection: case 1: basic.challenges01() @@ -42,7 +42,7 @@ def basic_challenges(): case 5: basic.challenges05() case 6: - pass + basic.challenges06() case 7: pass case 'v':