Algo de formato

This commit is contained in:
perro tuerto 2023-04-20 10:08:00 -07:00
parent f8112e53a4
commit 7993456cb4
13 changed files with 118 additions and 38 deletions

View File

@ -1,9 +1,6 @@
""" """
# hello.py Your First Program
Page 11
The window that appears should contain a cursor awaiting your input,
but its different from the interactive shell, which runs Python instructions
Python Basics as soon as you press enter.
""" """
print("Nombre:") print("Nombre:")

View File

@ -1,16 +1,6 @@
""" """
# collatz() Practice Projects
Page 76
Write a function named collatz() that has one parameter named number. If
number is even, then collatz() should print number / 2 and return this value.
If number is odd, then collatz() should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that
keeps calling collatz() on that number until the function returns the value 1.
(Amazingly enough, this sequence actually works for any integersooner or
later, using this sequence, youll arrive at 1! Even mathematicians arent sure
why. Your program is exploring whats called the Collatz sequence, sometimes
called the simplest impossible math problem.)
""" """
import time import time

View File

@ -1,3 +1,9 @@
"""
Practice Projects: Comma Code
Page 107
"""
def add(ls): def add(ls):
if len(ls) > 0: if len(ls) > 0:
ls = list(map(lambda x: str(x), ls)) ls = list(map(lambda x: str(x), ls))

View File

@ -1,3 +1,8 @@
"""
Practice Projects: Coin Flip Streaks
Page 107
"""
import random import random

View File

@ -1,3 +1,8 @@
"""
Practice Projects: Character Picture Grid
Page 108
"""
grid = [ grid = [
[".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", "."],
[".", "0", "0", ".", ".", "."], [".", "0", "0", ".", ".", "."],

View File

@ -1,3 +1,8 @@
"""
Practice Projects: Chess Dictionary Validator
Page 127
"""
import sys import sys
tests = [ tests = [

View File

@ -1,8 +1,13 @@
"""
Practice Projects: Fantasy Game Inventory
Page 127
"""
import sys import sys
tests = [ tests = [
{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}, {"rope": 1, "torch": 6, "gold coin": 42, "dagger": 1, "arrow": 12},
{'rope': 1, 'torch': 6, 'gold coin': "a", 'dagger': 1, 'arrow': 12}, {"rope": 1, "torch": 6, "gold coin": "a", "dagger": 1, "arrow": 12},
None, None,
] ]

View File

@ -1,9 +1,14 @@
"""
Practice Projects: List to Dictionary Function for Fantasy Game Inventory
Page 128
"""
import sys import sys
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] dragonLoot = ["gold coin", "dagger", "gold coin", "gold coin", "ruby"]
tests = [ tests = [
{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}, {"rope": 1, "torch": 6, "gold coin": 42, "dagger": 1, "arrow": 12},
{'rope': 1, 'torch': 6, 'gold coin': "a", 'dagger': 1, 'arrow': 12}, {"rope": 1, "torch": 6, "gold coin": "a", "dagger": 1, "arrow": 12},
None, None,
] ]

View File

@ -1,3 +1,8 @@
"""
Practice Projects: Table Printer
Page 154
"""
tableData = [ tableData = [
["apples", "oranges", "cherries", "banana"], ["apples", "oranges", "cherries", "banana"],
["Alice", "Bob", "Carol", "David"], ["Alice", "Bob", "Carol", "David"],

View File

@ -1,3 +1,8 @@
"""
Practice Projects: Zombie Dice Bots
Page 155
"""
import random import random
import zombiedice import zombiedice

View File

@ -1,6 +1,12 @@
"""
Project: Phone Number and Email Address Extractor
Page 179
"""
import re import re
import pyperclip import pyperclip
def to_int(string): def to_int(string):
string = re.sub(r"\D", "", string) string = re.sub(r"\D", "", string)
if string: if string:
@ -8,18 +14,22 @@ def to_int(string):
else: else:
return None return None
def rgx_phones(): def rgx_phones():
return re.compile( return re.compile(
r"((\+(\d+)\s*)?(([ |\d|\-|\.|\(|\)]){7,}\d)(\s+ext\S*\s+(\d+))?)", re.I r"((\+(\d+)\s*)?(([ |\d|\-|\.|\(|\)]){7,}\d)(\s+ext\S*\s+(\d+))?)",
re.I,
) )
def rgx_emails(): def rgx_emails():
return re.compile(r"[\w\-]+@[\w\-]+\.\w{2,}", re.I) return re.compile(r"[\w\-]+@[\w\-]+\.\w{2,}", re.I)
def parse_phone(phone): def parse_phone(phone):
lbraces = len(re.findall(r'.*\(.*', phone[0])) lbraces = len(re.findall(r".*\(.*", phone[0]))
rbraces = len(re.findall(r'.*\).*', phone[0])) rbraces = len(re.findall(r".*\).*", phone[0]))
if lbraces == rbraces and re.match(r'^[\+|\d]', phone[0]): if lbraces == rbraces and re.match(r"^[\+|\d]", phone[0]):
code = to_int(phone[2]) code = to_int(phone[2])
ext = to_int(phone[6]) ext = to_int(phone[6])
res = to_int(phone[3]) res = to_int(phone[3])
@ -28,9 +38,11 @@ def parse_phone(phone):
res = f"{res} ext. {ext}" if ext else res res = f"{res} ext. {ext}" if ext else res
return res return res
def get_phones(content): def get_phones(content):
return list(filter(None, map(parse_phone, rgx_phones().findall(content)))) return list(filter(None, map(parse_phone, rgx_phones().findall(content))))
content = pyperclip.paste() content = pyperclip.paste()
phones = get_phones(content) phones = get_phones(content)
emails = rgx_emails().findall(content) emails = rgx_emails().findall(content)

View File

@ -1,29 +1,66 @@
"""
Project: Sandwich Maker
Page 198
"""
import pyinputplus as pyip import pyinputplus as pyip
prices, order, total = { prices, order, total = (
"bread": { "wheat": 1.0, "white": .9, "sourdough": 1.1, }, {
"protein": { "chicken": 1.0, "turkey": 1.1, "ham": .9, "tofu": .8, }, "bread": {
"cheese": { "cheddar": .9, "swiss": 1.0, "mozzarella.": 1.1, } "wheat": 1.0,
}, {}, 0 "white": 0.9,
"sourdough": 1.1,
},
"protein": {
"chicken": 1.0,
"turkey": 1.1,
"ham": 0.9,
"tofu": 0.8,
},
"cheese": {
"cheddar": 0.9,
"swiss": 1.0,
"mozzarella.": 1.1,
},
},
{},
0,
)
def add(key, ismenu=False): def add(key, ismenu=False):
isadd = pyip.inputYesNo(prompt=f"Do you want {key}? [y/n]\n") == "yes" isadd = pyip.inputYesNo(prompt=f"Do you want {key}? [y/n]\n") == "yes"
if isadd and ismenu: if isadd and ismenu:
return pyip.inputMenu(list(prices[key].keys()), numbered=True, prompt=f"Please, select {key}:\n") return pyip.inputMenu(
list(prices[key].keys()),
numbered=True,
prompt=f"Please, select {key}:\n",
)
elif isadd: elif isadd:
return True return True
else: else:
return False return False
order["bread"] = pyip.inputMenu(list(prices["bread"].keys()), numbered=True, prompt="Please, select bread:\n") order["bread"] = pyip.inputMenu(
order["protein"] = pyip.inputMenu(list(prices["protein"].keys()), numbered=True, prompt="Please, select protein:\n") list(prices["bread"].keys()),
numbered=True,
prompt="Please, select bread:\n",
)
order["protein"] = pyip.inputMenu(
list(prices["protein"].keys()),
numbered=True,
prompt="Please, select protein:\n",
)
order["cheese"] = add("cheese", True) order["cheese"] = add("cheese", True)
order["mayo"] = add("mayo") order["mayo"] = add("mayo")
order["mustard"] = add("mustard") order["mustard"] = add("mustard")
order["lettuce"] = add("lettuce") order["lettuce"] = add("lettuce")
order["tomato"] = add("tomato") order["tomato"] = add("tomato")
order["quantity"] = pyip.inputInt(prompt="How many sandwiches? [number]:\n", greaterThan=0) order["quantity"] = pyip.inputInt(
prompt="How many sandwiches? [number]:\n", greaterThan=0
)
sandwich = "sandwich" if order["quantity"] == 1 else "sandwiches" sandwich = "sandwich" if order["quantity"] == 1 else "sandwiches"
print(f"Your order\n{order['quantity']} {sandwich} with:") print(f"Your order\n{order['quantity']} {sandwich} with:")
@ -39,9 +76,8 @@ for key in order.keys():
else: else:
print(f"{prompt}NO {key}") print(f"{prompt}NO {key}")
subtotal = round(total * order["quantity"], 2) subtotal = round(total * order["quantity"], 2)
taxes = round(subtotal * .16, 2) taxes = round(subtotal * 0.16, 2)
total = round(subtotal + taxes, 2) total = round(subtotal + taxes, 2)
print(f"SUBTOTAL: {subtotal}") print(f"SUBTOTAL: {subtotal}")
print(f"TAXES: {taxes}") print(f"TAXES: {taxes}")
print(f"TOTAL: {total}") print(f"TOTAL: {total}")

View File

@ -0,0 +1,4 @@
"""
Project: Write Your Own Multiplication Quiz
Page 199
"""