python_by_example/basic/basic05.py

110 lines
4.4 KiB
Python
Raw Permalink Normal View History

2023-11-09 20:37:02 -03:00
class basics_005:
def name_three(self):
"Ask the user to enter their name and then display their name three times."
name = input("Ingresa tu nombre: ")
for _ in range(3):
print(name)
def name_num(self):
"""Alter program 035 so that it will ask the user to enter their name and
a number and then display their name that number of times."""
name = input("Ingresa tu nombre: ")
num = int(input("Ingresa un número: "))
for _ in range(num):
print(name)
def name_nl(self):
"""Ask the user to enter their name and display each letter in their name
on a separate line."""
name = input("Ingresa tu nombre: ")
for char in name:
print(char)
def name_nl_num(self):
"""Change program 037 to also ask for a number. Display their name (one
letter at a time on each line) and repeat this for the number of times
they entered."""
name = input("Ingresa tu nombre: ")
num = int(input("Ingresa un número: "))
for _ in range(num):
for char in name:
print(char)
def times_table(self):
"""Ask the user to enter a number between 1 and 12 and then display the
times table for that number."""
num = int(input("Ingresa un número entre 1 y 12: "))
for i in range(1,13):
res = num*i
print(f'{i} x {num} = {res}')
def count_down(self):
"""Ask for a number below 50 and then count down from 50 to that number,
making sure you show the number they entered in the output."""
from time import sleep
num = int(input("Ingresa un número inferior a 50: "))
for i in range(50, num-1, -1):
print(f'{num}: {i}')
sleep(0.05)
def mambo_name(self):
"""Ask the user to enter their name and a number. If the number is less
than 10, then display their name that number of times; otherwise display
the message \"Too high\" three times."""
name = input("Ingresa tu nombre: ")
num = int(input("Ingresa un número: "))
if num < 10:
for _ in range(num):
print(name)
else:
for _ in range(3):
print("Muy alto")
def may_add(self):
"""Set a variable called total to 0. Ask the user to enter five numbers
and after each input ask them if they want that number included. If
they do, then add the number to the total. If they do not want it
2023-11-17 13:26:09 -03:00
included, don't add it to the total. After they have entered all five
2023-11-09 20:37:02 -03:00
numbers, display the total."""
total = 0
for _ in range(5):
tmp = int(input("Ingresa un número: "))
res = input("¿Quieres agregarlo al total? [s|n]").lower()
if res == 's':
total += tmp
print(f"El total es: {total}")
def up_or_down(self):
"""Ask which direction the user wants to count (up or down). If they
select up, then ask them for the top number and then count from 1 to
that number. If they select down, ask them to enter a number below 20
and then count down from 20 to that number. If they entered something
2023-11-17 13:26:09 -03:00
other than up or down, display the message \"I don't understand\"."""
2023-11-09 20:37:02 -03:00
from time import sleep
toward = input("¿Prefieres contar hacia ARRIBA o hacia ABAJO?: ").lower()
if toward == 'arriba':
num = int(input("Ingresa un número límite: "))
for i in range(1, num+1):
print(i)
elif toward == 'abajo':
num = int(input("Ingresa un número menor a 20: "))
for i in range(20, num-1, -1):
print(i)
sleep(0.05)
else:
print("No te entiendo")
def guests(self):
"""Ask how many people the user wants to invite to a party. If they enter
a number below 10, ask for the names and after each name display \"[name]
has been invited\". If they enter a number which is 10 or higher, display
the message \"Too many people\"."""
num = int(input("¿Cuantas personas quieres invitar a la fiesta?: "))
if num < 10:
for _ in range(num):
name = input("Ingresa el nombre del invitado: ")
print(f'{name} ha sido invitado')
else:
print("Demasiadas personas")