Add more exercises, update PCCH to use CLI arguments

This commit is contained in:
Josh Mudge 2018-09-29 16:50:38 -06:00
parent 50f6f29067
commit 1ca295566d
5 changed files with 94 additions and 3 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
out.txt out.txt
Python3HardWay.pdf

View File

@ -1,10 +1,21 @@
# Python Caeser Cipher Helper v1.0.1
# Usage (encrypt): python3 caeser-cipher.py encrypt secretmessage
# Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage
import re # Import Regex tools import re # Import Regex tools
import string # Import string tools import string # Import string tools
import collections import collections # Import tools to sort letters.
from sys import argv # Import tool to process command line arguments.
ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user. #if action == "help": # If error, print help https://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu
#print("\nUsage (encrypt): python3 caeser-cipher.py encrypt secretmessage")
#print("Usage (decrypt): python3 caeser-cipher.py decrypt secretmessage\n")
script, action, ciphertext = argv # Take command line input.
#ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user.
shiftNum = input("Please enter the shift used (Just hit enter if you don't know):") # Get input shiftNum = input("Please enter the shift used (Just hit enter if you don't know):") # Get input
action = input("Please enter encrypt or decrypt):") # Get input #action = input("Please enter encrypt or decrypt):") # Get input
ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters. ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters.
ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/
letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet

39
ex12_ss.py Normal file
View File

@ -0,0 +1,39 @@
age = int(input("How old are you?" ))
#age = print("How old are you?" , input()) # It's asking for input before prompting the user.
height = input(f"You're {age}? Nice. How tall are you? ")
weight = input("How much do you weigh? ")
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
# {age} printed in absence of f
# open() allows you to open files in this syntax:
#open(name[, mode[, buffering]]) -> file object
# Likea so: with open('out.txt', 'w') as f: # W = write f = var to be used later.
# print("." * 1000000000, file=f) # A little fun with a billion dots being saved to a file.
#file(name[, mode[, buffering]]) -> file object
#
# Pydoc:
# Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
# | writing or appending. The file will be created if it doesn't exist
# | when opened for writing or appending; it will be truncated when
# | opened for writing. Add a 'b' to the mode for binary files.
# | Add a '+' to the mode to allow simultaneous reading and writing.
# | If the buffering argument is given, 0 means unbuffered, 1 means line
# | buffered, and larger numbers specify the buffer size. The preferred way
# | to open a file is with the builtin open() function.
# | Add a 'U' to mode to open the file for input with universal newline
# | support. Any line ending in the input file will be seen as a '\n'
# | in Python. Also, a file so opened gains the attribute 'newlines';
# | the value for this attribute is one of None (no newline read yet),
# | '\r', '\n', '\r\n' or a tuple containing all the newline types seen.
# |
# | 'U' cannot be combined with 'w' or '+' mode.
# os.curdir is a string representing the current directory ('.' or ':')
# os.pardir is a string representing the parent directory ('..' or '::')
# - os.pathsep is the component separator used in $PATH etc
# - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')

14
ex13.py Normal file
View File

@ -0,0 +1,14 @@
from sys import argv # From sys module, grab the argv feature.
script, first, second, third, itsaboy = argv # Mapping command line args. $0, $1, $2 $3 name of script, 3 variables
first = input("You are a cow. Tell me why.")
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variables is:", second)
print("Your third variable is:", third)
print("Your fourth variable is:", itsaboy)
print(">>> argv=", repr(argv))
# You can break it by not putting the right amount of variables.
hi = input("You are a cow. Tell me why.")
print(hi)

26
ex14.py Normal file
View File

@ -0,0 +1,26 @@
from sys import argv
script, user_name, firstcow = argv
prompt = '> '
print("How old are you?")
age = int(input(prompt))
print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
print("Why are you a cow?")
likes = input(prompt)
print(f"Where do you live {user_name}?")
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print(f"""
Alright, so you said you're {age} and {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have {computer} computer. Nice.
""")
print(firstcow)