From 1ca295566db6635c91b81aafb0110ba38ff9575d Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Sat, 29 Sep 2018 16:50:38 -0600 Subject: [PATCH] Add more exercises, update PCCH to use CLI arguments --- .gitignore | 1 + caeser-cipher.py | 17 ++++++++++++++--- ex12_ss.py | 39 +++++++++++++++++++++++++++++++++++++++ ex13.py | 14 ++++++++++++++ ex14.py | 26 ++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 ex12_ss.py create mode 100644 ex13.py create mode 100644 ex14.py diff --git a/.gitignore b/.gitignore index 9cd6546..1004750 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ out.txt +Python3HardWay.pdf diff --git a/caeser-cipher.py b/caeser-cipher.py index 3b014cf..81ea07e 100644 --- a/caeser-cipher.py +++ b/caeser-cipher.py @@ -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 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 -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 = 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 diff --git a/ex12_ss.py b/ex12_ss.py new file mode 100644 index 0000000..7f5129d --- /dev/null +++ b/ex12_ss.py @@ -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') diff --git a/ex13.py b/ex13.py new file mode 100644 index 0000000..88dd3b3 --- /dev/null +++ b/ex13.py @@ -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) diff --git a/ex14.py b/ex14.py new file mode 100644 index 0000000..2945d84 --- /dev/null +++ b/ex14.py @@ -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)