Skip to content

Commit a672026

Browse files
committed
First
1 parent af4d882 commit a672026

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1863
-0
lines changed

edition_1/.goutputstream-M1ELIW

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#07_11_canvas.py
2+
3+
from tkinter import *
4+
5+
class App:
6+
7+
def __init__(self, master):
8+
canvas = Canvas(master, width=400, height=200)
9+
canvas.pack()
10+
canvas.create_rectangle(20, 20, 300, 100, fill='blue')
11+
canvas.create_oval(30, 50, 290, 190, fill='#ff2277')
12+
canvas.create_line(0, 0, 400, 200, fill='black', width=5)
13+
root = Tk()
14+
app = App(root)
15+
root.mainloop()

edition_1/03_01_dice.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#03_01_dice
2+
import random
3+
for x in range(1, 11):
4+
random_number = random.randint(1, 6)
5+
print(random_number)

edition_1/03_02_double_dice.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#03_02_double_dice
2+
import random
3+
for x in range(1, 11):
4+
throw_1 = random.randint(1, 6)
5+
throw_2 = random.randint(1, 6)
6+
total = throw_1 + throw_2
7+
print(total)
8+
if total == 7:
9+
print('Seven Thrown!')
10+
if total == 11:
11+
print('Eleven Thrown!')
12+
if throw_1 == throw_2:
13+
print('Double Thrown!')
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#03_03_double_dice_solution
2+
import random
3+
for x in range(1, 11):
4+
throw_1 = random.randint(1, 6)
5+
throw_2 = random.randint(1, 6)
6+
total = throw_1 + throw_2
7+
print(total)
8+
if total == 7:
9+
print('Seven Thrown!')
10+
if total == 11:
11+
print('Eleven Thrown!')
12+
if throw_1 == throw_2:
13+
print('Double Thrown!')
14+
if total >= 5 and total <= 9:
15+
print('Not Bad!')
16+
if total > 10:
17+
print('Good Throw!')
18+
if total < 4:
19+
print('Unlucky!')

edition_1/03_04_double_dice_while.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#03_04_double_dice_while
2+
import random
3+
throw_1 = random.randint(1, 6)
4+
throw_2 = random.randint(1, 6)
5+
while not (throw_1 == 6 and throw_2 == 6):
6+
total = throw_1 + throw_2
7+
print(total)
8+
throw_1 = random.randint(1, 6)
9+
throw_2 = random.randint(1, 6)
10+
print('Double Six thrown!')
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#03_05_double_dice_while_break
2+
import random
3+
while True:
4+
throw_1 = random.randint(1, 6)
5+
throw_2 = random.randint(1, 6)
6+
total = throw_1 + throw_2
7+
print(total)
8+
if throw_1 == 6 and throw_2 == 6:
9+
break
10+
print('Double Six thrown!')

edition_1/04_01_list_and_for.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#04_01_list_and_for
2+
list = [1, 'one', 2, True]
3+
for item in list:
4+
print(item)

edition_1/04_02_polite_function.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#04_02_polite_function
2+
def make_polite(sentence):
3+
polite_sentence = sentence + ' please'
4+
return polite_sentence
5+
6+
print(make_polite('Pass the salt'))
7+

edition_1/04_03_hello_n.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#04_03_hello_n
2+
def say_hello(n):
3+
for x in range(0, n):
4+
print('Hello')
5+
6+
say_hello(5)

edition_1/04_04_hangman_words.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#04_04_hangman_words
2+
import random
3+
4+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
5+
6+
def pick_a_word():
7+
word_position = random.randint(0, len(words) - 1)
8+
return words[word_position]
9+
10+
print(pick_a_word())

edition_1/04_05_hangman_play.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#04_05_hangman_play
2+
3+
import random
4+
5+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
6+
lives_remaining = 14
7+
8+
def play():
9+
word = pick_a_word()
10+
while True:
11+
guess = get_guess(word)
12+
if process_guess(guess, word):
13+
print('You win! Well Done!')
14+
break
15+
if lives_remaining == 0:
16+
print('You are Hung!')
17+
print('The word was: ' + word)
18+
break
19+
20+
def pick_a_word():
21+
word_position = random.randint(0, len(words) - 1)
22+
return words[word_position]
23+
24+
def get_guess(word):
25+
return 'a'
26+
27+
def process_guess(guess, word):
28+
global lives_remaining
29+
lives_remaining = lives_remaining -1
30+
return False
31+
32+
play()

edition_1/04_06_hangman_get_guess.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#04_06_hangman_get_guess
2+
3+
import random
4+
5+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
6+
lives_remaining = 14
7+
8+
def play():
9+
word = pick_a_word()
10+
while True:
11+
guess = get_guess(word)
12+
if process_guess(guess, word):
13+
print('You win! Well Done!')
14+
break
15+
if lives_remaining == 0:
16+
print('You are Hung!')
17+
print('The word was: ' + word)
18+
break
19+
20+
def pick_a_word():
21+
word_position = random.randint(0, len(words) - 1)
22+
return words[word_position]
23+
24+
def get_guess(word):
25+
print_word_with_blanks(word)
26+
print('Lives Remaining: ' + str(lives_remaining))
27+
guess = input(' Guess a letter or whole word?')
28+
return guess
29+
30+
def process_guess(guess, word):
31+
global lives_remaining
32+
lives_remaining = lives_remaining -1
33+
return False
34+
35+
def print_word_with_blanks(word):
36+
print('print_word_with_blanks: not done yet')
37+
38+
play()

edition_1/04_07_hangman_print_word.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#04_07_hangman_print_word
2+
3+
import random
4+
5+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
6+
lives_remaining = 14
7+
guessed_letters = ''
8+
9+
def play():
10+
word = pick_a_word()
11+
while True:
12+
guess = get_guess(word)
13+
if process_guess(guess, word):
14+
print('You win! Well Done!')
15+
break
16+
if lives_remaining == 0:
17+
print('You are Hung!')
18+
print('The word was: ' + word)
19+
break
20+
21+
def pick_a_word():
22+
word_position = random.randint(0, len(words) - 1)
23+
return words[word_position]
24+
25+
def get_guess(word):
26+
print_word_with_blanks(word)
27+
print('Lives Remaining: ' + str(lives_remaining))
28+
guess = input(' Guess a letter or whole word?')
29+
return guess
30+
31+
def process_guess(guess, word):
32+
global lives_remaining
33+
global guessed_letters
34+
lives_remaining = lives_remaining - 1
35+
guessed_letters = guessed_letters + guess
36+
return False
37+
38+
def print_word_with_blanks(word):
39+
display_word = ''
40+
for letter in word:
41+
if guessed_letters.find(letter) > -1:
42+
# letter found
43+
display_word = display_word + letter
44+
else:
45+
# letter not found
46+
display_word = display_word + '-'
47+
print(display_word)
48+
49+
play()

edition_1/04_08_hangman_full.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#04_08_hangman_full
2+
import random
3+
4+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
5+
lives_remaining = 14
6+
guessed_letters = ''
7+
8+
9+
10+
def play():
11+
word = pick_a_word()
12+
while True:
13+
guess = get_guess(word)
14+
if process_guess(guess, word):
15+
print('You win! Well Done!')
16+
break
17+
if lives_remaining == 0:
18+
print('You are Hung!')
19+
print('The word was: ' + word)
20+
break
21+
22+
def pick_a_word():
23+
word_position = random.randint(0, len(words) - 1)
24+
return words[word_position]
25+
26+
def get_guess(word):
27+
print_word_with_blanks(word)
28+
print('Lives Remaining: ' + str(lives_remaining))
29+
guess = input(' Guess a letter or whole word?')
30+
return guess
31+
32+
def print_word_with_blanks(word):
33+
display_word = ''
34+
for letter in word:
35+
if guessed_letters.find(letter) > -1:
36+
# letter found
37+
display_word = display_word + letter
38+
else:
39+
# letter not found
40+
display_word = display_word + '-'
41+
print(display_word)
42+
43+
def process_guess(guess, word):
44+
if len(guess) > 1:
45+
return whole_word_guess(guess, word)
46+
else:
47+
return single_letter_guess(guess, word)
48+
49+
50+
def whole_word_guess(guess, word):
51+
global lives_remaining
52+
if guess == word:
53+
return True
54+
else:
55+
lives_remaining = lives_remaining - 1
56+
return False
57+
58+
def single_letter_guess(guess, word):
59+
global guessed_letters
60+
global lives_remaining
61+
if word.find(guess) == -1:
62+
# letter guess was incorrect
63+
lives_remaining = lives_remaining - 1
64+
guessed_letters = guessed_letters + guess
65+
if all_letters_guessed(word):
66+
return True
67+
return False
68+
69+
def all_letters_guessed(word):
70+
for letter in word:
71+
if guessed_letters.find(letter) == -1:
72+
return False
73+
return True
74+
75+
play()
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#04_09_hangman_full_solution
2+
import random
3+
4+
words = ['chicken', 'dog', 'cat', 'mouse', 'frog']
5+
lives_remaining = 14
6+
guessed_letters = ''
7+
8+
9+
10+
def play():
11+
word = pick_a_word()
12+
while True:
13+
guess = get_guess(word)
14+
if process_guess(guess, word):
15+
print('You win! Well Done!')
16+
break
17+
if lives_remaining == 0:
18+
print('You are Hung!')
19+
print('The word was: ' + word)
20+
break
21+
22+
def pick_a_word():
23+
word_position = random.randint(0, len(words) - 1)
24+
return words[word_position]
25+
26+
def get_guess(word):
27+
print_word_with_blanks(word)
28+
print('Lives Remaining: ' + str(lives_remaining))
29+
guess = input(' Guess a letter or whole word?')
30+
return guess
31+
32+
def print_word_with_blanks(word):
33+
display_word = ''
34+
for letter in word:
35+
if guessed_letters.find(letter) > -1:
36+
# letter found
37+
display_word = display_word + letter
38+
else:
39+
# letter not found
40+
display_word = display_word + '-'
41+
print(display_word)
42+
43+
def process_guess(guess, word):
44+
if len(guess) > 1 and len(guess) == len(word):
45+
return whole_word_guess(guess, word)
46+
else:
47+
return single_letter_guess(guess, word)
48+
49+
50+
def whole_word_guess(guess, word):
51+
global lives_remaining
52+
if guess.lower() == word.lower():
53+
return True
54+
else:
55+
lives_remaining = lives_remaining - 1
56+
return False
57+
58+
def single_letter_guess(guess, word):
59+
global guessed_letters
60+
global lives_remaining
61+
if word.find(guess) == -1:
62+
# letter guess was incorrect
63+
lives_remaining = lives_remaining - 1
64+
guessed_letters = guessed_letters + guess.lower()
65+
if all_letters_guessed(word):
66+
return True
67+
return False
68+
69+
def all_letters_guessed(word):
70+
for letter in word:
71+
if guessed_letters.find(letter.lower()) == -1:
72+
return False
73+
return True
74+
75+
play()

0 commit comments

Comments
 (0)