Skip to content

Commit 16fe041

Browse files
committed
modified LPTHW
1 parent ec16773 commit 16fe041

25 files changed

+1277
-10
lines changed
5.9 MB
Binary file not shown.

codes/ex1.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ex01: A Good First Program
2+
3+
print("Hello World!")
4+
print("Hello Again")
5+
print("I like typing this.")
6+
print("This is fun.")
7+
print('Yay! Printing.')
8+
print("I'd much rather you 'not'.")
9+
print('I "said" do not touch this.')
10+
11+
12+
13+
#study drills
14+
15+
# ex01: A Good First Program
16+
17+
print("Hello World!")
18+
print("Hello Again")
19+
print("I like typing this.")
20+
print("This is fun.")
21+
print('Yay! Printing.')
22+
print("I'd much rather you 'not'.")
23+
# print('I "said" do not touch this.')
24+
print('This is another line!')

codes/ex10.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,43 @@
1111
print(persian_cat)
1212
print(backslash_cat)
1313
print(fat_cat)
14-
print("\a"+"\n"+"\b"+"\n"+"\f"+"\n"+"\r"+"\n"+"\t"+"\n"+"\v")
14+
print("\a"+"\n"+"\b"+"\n"+"\f"+"\n"+"\r"+"\n"+"\t"+"\n"+"\v")
15+
16+
# study drills
17+
#!/usr/bin/env python3
18+
19+
# ex10: What Was That?
20+
21+
tabby_cat = "\tI'm tabbed in."
22+
persian_cat = "I'm split\non a line."
23+
backslash_cat = "I'm \\ a \\ cat."
24+
25+
fat_cat = '''
26+
I'll do a list:
27+
\t* Cat food
28+
\t* Fishies
29+
\t* Catnip\n\t* Grass
30+
'''
31+
32+
print(tabby_cat)
33+
print(persian_cat)
34+
print(backslash_cat)
35+
print(fat_cat)
36+
37+
# Assign string value for each variable
38+
intro = "I'll print a week:"
39+
mon = "Mon"
40+
tue = "Tue"
41+
wed = "Wed"
42+
thu = "Thu"
43+
fri = "Fri"
44+
sat = "Sat"
45+
sun = "Sun"
46+
47+
print("%s\n%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (intro, mon, tue, wed, thu, fri, sat, sun))
48+
49+
print("%r" % intro)
50+
print("%r" % "She said \"I'll print a week\"")
51+
52+
print("%s" % intro)
53+
print("%s" % "She said \"I'll print a week\"")

codes/ex11.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,52 @@
66
weight = input()
77

88
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
9+
10+
# study drills
11+
12+
#!/usr/bin/env python3
13+
14+
# ex11: Asking Questions
15+
16+
# raw_input() was renamed to input() in Python v3.x,
17+
# and the old input() is gone, but you can emulate it with eval(input())
18+
19+
print("How old are you?", end=" ")
20+
age = input()
21+
print("How tall are you?", end=" ")
22+
height = input()
23+
print("How much do you weight", end=" ")
24+
weight = input()
25+
26+
print("So, you're %r old, %r tall and %r heavy." % (age, height, weight))
27+
28+
print("Enter a integer: ", end=" ")
29+
num = int(eval(input())) # won't work with int(raw_input)), with eval(input()) it would work
30+
print("The number you've input is: %d" % num)
31+
print("Enter a name: ", end=" ")
32+
name = input()
33+
print("What's %s's age?" % name, end=" ")
34+
age = eval(input())
35+
print("What's %s's height?" % name, end=" ")
36+
height = eval(input())
37+
print("What's %s's weight?" % name, end=" ")
38+
weight = eval(input())
39+
print("What's the color of %s's eyes?" % name, end=" ")
40+
eyes = input()
41+
print("What's the color of %s's teeth?" % name, end=" ")
42+
teeth = input()
43+
print("What's the color of %s's hair?" % name, end=" ")
44+
hair = input()
45+
46+
type(name) # the data type of name will be <class 'str'>
47+
type(age) # the data type of age will be <class 'int'>
48+
49+
print("Let's talk about %s" % name)
50+
print("He's %d years old." % age)
51+
print("He's %d inches tall." % height)
52+
print("He's %d pounds heavy." % weight)
53+
print("Actually that's not too heavy")
54+
print("He's got %s eyes and %s hair." % (eyes, hair))
55+
print("His teeth are usually %s depending on the coffee." % (teeth))
56+
57+
print('If I add %d, %d and %d I get %d.' % (age, height, weight, age + height + weight))

codes/ex13.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
3+
# ex13: Parameters, Unpacking, Variables
4+
5+
from sys import argv
6+
7+
script, first, second, third = argv
8+
9+
print("The script is called:", script)
10+
print("Your first variable is:", first)
11+
print("Your second variable is:", second)
12+
print("Your third variable is:", third)
13+
14+
#study drills 1
15+
16+
# ex13: Parameters, Unpacking, Variables
17+
# Write a script that has fewer arguments
18+
19+
from sys import argv
20+
21+
script, first_name, last_name = argv
22+
23+
print("The script is called:", script)
24+
print("Your first name is:", first_name)
25+
print("Your last name is:", last_name)
26+
27+
#study drill 2
28+
#!/usr/bin/env python3
29+
30+
# ex13: Parameters, Unpacking, Variables
31+
# Write a script that has more arguments.
32+
33+
from sys import argv
34+
35+
script, name, age, height, weight = argv
36+
37+
print("The script is called:", script)
38+
print("Your name is:", name)
39+
print("Your age is:", age)
40+
print("Your height is %d inches" % int(height))
41+
print("Your weight is %d pounds" % int(weight))
42+
43+
#study drill 3
44+
#!/usr/bin/env python3
45+
46+
# ex13: Parameters, Unpacking, Variables
47+
# Combine input with argv to make a script that gets more input from a user.
48+
49+
from sys import argv
50+
51+
script, first_name, last_name = argv
52+
53+
middle_name = input("What's your middle name?")
54+
55+
print("Your full name is %s %s %s." % (first_name, middle_name, last_name))

codes/ex14.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,33 @@
2020
And you have a {computer} computer. Nice.
2121
""")
2222

23+
# study drills
24+
25+
#!/usr/bin/env python3
26+
27+
# ex14: Prompting and Passing
28+
29+
from sys import argv
30+
31+
# Add another argument and use it in your script
32+
script, user_name, city = argv
33+
# Change the prompt variable to something else
34+
prompt = 'Please type the answer: '
35+
36+
print("Hi %s from %s, I'm the %s script." % (user_name, city, script))
37+
print("I'd like to ask you a few questions.")
38+
print("Do you like me %s?" % user_name)
39+
likes = input(prompt)
40+
41+
print("What's the whether like today in %s?" % city)
42+
weather = input(prompt)
43+
44+
print("What kind of computer do you have?")
45+
computer = input(prompt)
46+
47+
print("""
48+
Alright, so you said %r about liking me.
49+
The weather in your city is %s.
50+
But I can't feel it because I'm a robot.
51+
And you have a %r computer. Nice.
52+
""" % (likes, weather, computer))

codes/ex15.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,39 @@
1515
print(txt_again.read())
1616

1717

18+
# study drills
19+
20+
#!/usr/bin/env python3
21+
22+
# ex15: Reading Files
23+
24+
# import argv variables from sys submodule
25+
from sys import argv
26+
27+
# get the argv variables
28+
script, filename = argv
29+
30+
# open a file
31+
txt = open(filename)
32+
33+
# print file name
34+
print("Here's your file %r: " % filename)
35+
# print all the contents of the file
36+
print(txt.read())
37+
38+
# close the file
39+
txt.close()
40+
41+
# prompt to type the file name again
42+
print("Type the filename again:")
43+
# input the new file name
44+
file_again = input("> ")
45+
46+
# open the new selected file
47+
txt_again = open(file_again)
48+
49+
# print the contents of the new selected file
50+
print(txt_again.read())
51+
52+
# close the file
53+
txt_again.close()

codes/ex16.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,54 @@
3131

3232
print("And finally, we close it")
3333
target.close()
34+
35+
#study drills
36+
37+
#!/usr/bin/env python3
38+
39+
# ex16: Reading and Writing Files
40+
41+
from sys import argv
42+
43+
script, filename = argv
44+
45+
print("We're going to erase %r." % filename)
46+
print("If you don't want that, hit CTRL-C (^C).")
47+
print("If you do want that, hit RETURN.")
48+
49+
input("?")
50+
51+
print("Opening the file...")
52+
# Open this file in 'write' mode
53+
target = open(filename, 'w')
54+
55+
print("Truncating the file. Goodbye!")
56+
target.truncate()
57+
58+
print("Now I'm going to ask you for three lines.")
59+
60+
line1 = input("line 1: ")
61+
line2 = input("line 2: ")
62+
line3 = input("line 3: ")
63+
64+
print("I'm going to write these to the file.")
65+
66+
target.write("%s\n%s\n%s\n" % (line1, line2, line3))
67+
68+
print("And finally, we close it.")
69+
target.close()
70+
71+
72+
#!/usr/bin/env python3
73+
74+
# A script similar to ex16 that uses read and argv to read the file
75+
76+
from sys import argv
77+
78+
script, filename = argv
79+
80+
print("The contents of the file %s:" % filename)
81+
target = open(filename)
82+
contents = target.read()
83+
print(contents)
84+
target.close()

codes/ex17.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@
2323
in_file.close()
2424

2525

26+
#study drills
2627

28+
#!/usr/bin/env python3
29+
30+
# ex17: More Files
31+
32+
from sys import argv
33+
34+
script, from_file, to_file = argv
35+
36+
open(to_file, 'w').write(open(from_file).read())

0 commit comments

Comments
 (0)