Exercises 12-20 lp3thw

This commit is contained in:
Ganome 2021-04-19 20:24:26 +00:00
parent 0ca0039bd4
commit 347ec43263
9 changed files with 179 additions and 2 deletions

View File

@ -1,6 +1,6 @@
age = input("How old are you? ") age = input("How old are you? ")
height = input("How tall are you?") height = input("How tall are you? ")
weight = input("How much do you weight?") weight = input("How much do you weight? ")
print(f"So, you're {age} old, {height} tall, and {weight} heavy/") print(f"So, you're {age} old, {height} tall, and {weight} heavy/")

View File

@ -0,0 +1,9 @@
from sys import argv
script, first, second, third = argv
print("The script is called: ", script)
print(f"The first variable was: {first}")
print(f"The second variable was: {second}\nThe third variable was : {third}")
#To run this script you must call it from the terminal via - python ex13.py Arg1 Arg2 Arg3

View File

@ -0,0 +1,21 @@
#!/bin/python3
from sys import argv
script, userName, likeLevel = argv
prompt = "Well? - "
print(f"Hi {userName}, I'm the {script} script")
print("I'd like to ask you a few questions: ")
print(f"Do you like me {userName}")
likes = input(prompt)
print(f"Where you live {userName}?")
lives = input(prompt)
print(f"What kind of Computer do you have?")
computer = input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
Your likeness level was {likeLevel}. What did i do?
You reside in {lives}. Where is that exactly?
And you have a {computer} computer.
""")

View File

@ -0,0 +1,18 @@
from sys import argv
script, filename = argv
txt = open(filename)
print(f"Here's your file {filename}")
print(txt.read())
print("Type the filename again: ")
fileAgain = input("> ")
txtAgain = open(fileAgain)
print(txtAgain.read())
#It seems that you can only have one active file stream open at a time. So in order to print the txt variable, we would need to redefine it.
#txt = open(filename)
#print(txt.read())

View File

@ -0,0 +1,35 @@
from sys import argv
script, filename = argv
print(f"We're going to erase {filename}.")
print("If you want to cancel, HIT CTRL-C (^C) NOW!!!")
print("If you want to proceed, hit Enter")
input("?")
print(f"Opening the file...{filename}")
target = open(filename, 'a')
#https://docs.python.org/3/library/functions.html#open documentation on open()
#print("Truncating the file. Goodbye!")
#target.truncate()
print("Now I'm going to ask you a write a few lines: ")
Q1 = input("Line 1: ")
Q2 = input("Line 2: ")
Q3 = input("Line 3: ")
print(f"Now I'll write the 3 lines to {filename}")
allQ = Q1 + "\n" + Q2 + "\n" + Q3 + "\n"
target.write(allQ)
#target.write("\n")
#target.write(Q2)
#target.write("\n")
#target.write(Q3)
#target.write("\n")
print(f"Finished writing, closing {filename}.")
target.close()

View File

@ -0,0 +1,22 @@
from sys import argv
from os.path import exists
script, fromFile, toFile = argv
print(f"Copying from {fromFile} into {toFile}")
inFile = open(fromFile)
inData = inFile.read()
print(f"The input file is {len(inData)} bytes long.")
print(f"Does the output file exist? {exists(toFile)}")
print("Ready? hit RETURN to continue - ")
input(" ------ ")
outFile = open(toFile, "w") #use mode to so we make sure its a clean copy. Erases the file on opening
outFile.write(inData)
outFile.close()
inFile.close()

View File

@ -0,0 +1,18 @@
def printTwo(*args):
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")
def printTwoAgain(arg1, arg2):
print(f"arg1: {arg1}. arg2: {arg2}")
def printOne(arg1):
print(f"arg1: {arg1}")
def printNone():
print("No arguments here")
printTwo("Ganome", "Mary")
printTwoAgain("Ganome", "Mary")
printOne("Single Argument")
printNone()

View File

@ -0,0 +1,21 @@
def cheeseAndCrackers(cheeseCount, boxesOfCrackers):
print(f"You have {cheeseCount} cheeses!")
print(f"You have {boxesOfCrackers} boxes of crackers")
print("Man thats enough for a party!")
print("Get a blanket!\n")
print("We can just give the function numbers directly")
cheeseAndCrackers(20, 30)
print("OR, we can use variables from our script:")
amountOfCheeses = 10
amountOfCrackers = 50
cheeseAndCrackers(amountOfCheeses, amountOfCrackers)
print("We can even do the math inside:")
cheeseAndCrackers(6 * 5, 24 + 16)
print("And we can combine the two, variables and math:")
cheeseAndCrackers(amountOfCheeses + 32,amountOfCrackers + 5 * 4)

View File

@ -0,0 +1,33 @@
from sys import argv
script, inputFile = argv
def printAll(f):
print(f.read())
def rewind(f):
f.seek(0)
def printALine(lineCount, f):
#print(lineCount, f.readline()) #THIS METHOD INCLUDES THE LINE NUMBER!!!
print(f.readline())
currentFile = open(inputFile)
print("First let's print the whole file: \n")
printAll(currentFile)
print("Now let's rewind. Kind of like a tape.")
rewind(currentFile)
print("Let's print three lines:")
currentLine = 1
printALine(currentLine, currentFile)
currentLine += 1
printALine(currentLine, currentFile)
currentLine = currentLine + 1 #THIS is identical to the line above!
printALine(currentLine, currentFile)