Skip to content

Commit 8606afd

Browse files
committed
Finished chapter 6
1 parent 24422e3 commit 8606afd

8 files changed

+94
-0
lines changed

06-str/bulletPointAdder.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#! python3
2+
# Adds Wikipedia bullet points to the start
3+
# of each line of text on the clipboard.
4+
5+
import pyperclip
6+
text = pyperclip.paste()
7+
8+
#Separate lines and add stars.
9+
lines = text.split('\n')
10+
for i in range(len(lines)): # loop through all indexes for "lines" list
11+
lines[i] = '* ' + lines[i] # add star to each string in "lines" list
12+
text = '\n'.join(lines)
13+
pyperclip.copy(text)

06-str/catnapping.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
print('''Dear Alice,
2+
3+
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
4+
5+
Sincerely,
6+
Bob''')

06-str/picnicTable.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def printPicnic(itemsDict, leftWidth, rightWidth):
2+
print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
3+
for k, v in itemsDict.items():
4+
print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth))
5+
6+
picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000}
7+
printPicnic(picnicItems, 12, 5)
8+
printPicnic(picnicItems, 20, 6)

06-str/printTable.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def getColWidth(table, widths):
2+
for i in range(len(table)):
3+
colWidth = 0
4+
for item in table[i]:
5+
widths[i] = max(widths[i], len(item))
6+
7+
8+
def printTable(table, widths):
9+
rowNum = len(table)
10+
colNum = len(table[0])
11+
for y in range(colNum):
12+
print(table[0][y].ljust(widths[0]), end=' ')
13+
for x in range(1, rowNum):
14+
print(table[x][y].ljust(widths[x]), end=' ')
15+
print()
16+
17+
18+
tableData = [['apples', 'oranges', 'cherries', 'banana'],
19+
['Alice', 'Bob', 'Carol', 'David'],
20+
['dogs', 'cats', 'moose', 'goose']]
21+
colWidths = [0] * len(tableData)
22+
23+
24+
getColWidth(tableData, colWidths)
25+
printTable(tableData, colWidths)

06-str/pw.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! python3
2+
# pw.py - An insecure password locker program.
3+
4+
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
5+
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
6+
'luggage': '12345'}
7+
8+
import sys, pyperclip
9+
if len(sys.argv) < 2:
10+
print('Usage: py pw.py [account] - copy account password')
11+
sys.exit()
12+
13+
account = sys.argv[1] # first command line arg is the account name
14+
15+
if account in PASSWORDS:
16+
pyperclip.copy(PASSWORDS[account])
17+
print('Password for ' + account + ' copied to clipboard.')
18+
else:
19+
print('There is no account named ' + account)

06-str/readme.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
�� Windows �ϣ� ����Դ���һ���������ļ������� Win-R ���д��ڣ� ������
2+
������򣨹����������ļ��ĸ�����Ϣ���μ���¼ B�������ļ��༭������������
3+
���룬 ����Ϊ pw.bat�� ���� C:\Windows Ŀ¼�£�
4+
@py.exe C:\Python34\pw.py %*
5+
@pause
6+
��������������ļ��� �� Windows �����п������򣬾�ֻҪ���� Win-R��
7+
������ pw <account name>��

06-str/usingPyperclip.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pyperclip #pip install pyperclip
2+
pyperclip.copy('Hello world!')
3+
pyperclip.paste()

06-str/validateInput.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
while True:
2+
print('Enter your age:')
3+
age = input()
4+
if age.isdecimal():
5+
break
6+
print('Please enter a number for your age.')
7+
8+
while True:
9+
print('Select a new password (letters and numbers only):')
10+
password = input()
11+
if password.isalnum():
12+
break
13+
print('Passwords can only have letters and numbers.')

0 commit comments

Comments
 (0)