python_by_example/basic/basic03.py

70 lines
3.0 KiB
Python
Raw Permalink Normal View History

2023-11-09 17:45:00 -03:00
class basics_003:
def name_length(self):
"""Ask the user to enter their first name and then display the length of
their name."""
name = input("Ingresa tu primer nombre: ")
print(f"{name} tiene {len(name)} letras")
def fullname_length(self):
"""Ask the user to enter their first name and then ask them to enter
their surname. Join them together with a space between and display the
name and the length of whole name."""
name = input("Ingresa tu primer nombre: ")
surname = input("Ingresa tu apellido: ")
length = len(name) + len(surname)
print(f"\'{name} {surname}\' tiene {length} letras")
def full_name_lower(self):
"""Ask the user to enter their first name and surname in lower case.
Change the case to title case and join them together. Display the
finished result."""
name = input("Ingresa tu primer nombre en minúsculas: ").title()
surname = input("Ingresa tu apellido en minúsculas: ").title()
print(f"{name} {surname}")
def nursery(self):
"""Ask the user to type in the first line of a nursery rhyme and display
the length of the string. Ask for a starting number and an ending number
and then display just that section of the text."""
rhyme = input("Escribe la primera linea de una canción de cuna: ")
print("Tiene {len(rhyme)} letras")
ini_num = int(input("Ingresa un número inicial"))
end_num = int(input("Ingresa un número final"))
sel = rhyme[ini_num:end_num]
print(sel)
def any_mayus(self):
"""Ask the user to type in any word and display it in upper case."""
anyword = input("Ingresa cualquier palabra: ").upper()
print(anyword)
def mambo_name(self):
"""Ask the user to enter their first name. If the length of their first
name is under five characters, ask them to enter their surname and join
them together (without a space) and display the name in upper case. If
the length of the first name is five or more characters, display their
first name in lower case."""
name = input("Ingresa tu primer nombre: ")
if len(name) < 5:
surname = input("Ingresa tu apellido: ")
fullname = (name+surname).upper()
print(fullname)
else:
print(name.lower())
def pig_latin(self):
"""Pig Latin takes the first consonant of a word, moves it to the end of
the word and adds on an \"ay\". If a word begins with a vowel you just
add \"way\" to the end. For example, pig becomes igpay, banana becomes
ananabay, and aadvark becomes aadvarkway. Create a program that will ask
the user to enter a word and change it into Pig Latin. Make sure the new
word is displayed in lower case."""
word = input("Ingresa una palabra: ").lower()
if word[0:1] in 'aeiou':
piglatin = word+'way'
else:
piglatin = word[1:]+word[0]+'ay'
print(piglatin)