Skip to content

Commit 867beca

Browse files
authored
After Lecture 5
1 parent 5eb4630 commit 867beca

File tree

5 files changed

+91
-22
lines changed

5 files changed

+91
-22
lines changed

Assessment/Wk4/high_low.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Nothing for a Pair (not in this game)
2+
# Amended after the images were created to add the option to loop the game
3+
# Amended to simplify the data entry
24

35
# imports
46
import random
@@ -17,39 +19,51 @@
1719
a_suite.append("Hearts")
1820
a_suite.append("Spades")
1921

22+
a_guesses={"H": "Higher", "L": "Lower", "": "Invalid Guess"}
23+
2024
s_success="Congratulations! You're a Winner!"
2125
s_failure="Better luck next time :("
2226
s_invalid="Sorry your guess wasn't understood"
2327

2428
print ( "Higher or Lower - Come on Down " )
2529
print ( "-" * 40)
2630

27-
# deal card
28-
i_card = random.randint(1,13)
29-
i_suite = random.randint(1,4)
31+
while True:
32+
# deal card
33+
i_card = random.randint(1,13)
34+
i_suite = random.randint(1,4)
3035

31-
print ( f'Your first card {i_card} is a {a_cards[i_card]} of {a_suite[i_suite]}')
36+
print ( f'Your first card {i_card} is a {a_cards[i_card]} of {a_suite[i_suite]}')
3237

33-
your_guess = input("Please enter if you think the next card will be 'higher' or 'lower' :").lower()
38+
your_guess = input("Please enter if you think the next card will be higher (H) or lower (L) :").upper()
3439

35-
print ( f'You guessed {your_guess} ')
40+
print ( f'You guessed {a_guesses[your_guess]} ')
3641

37-
# deal card
38-
i_card_2 = random.randint(1,13)
39-
i_suite_2 = random.randint(1,4)
42+
# deal card
43+
i_card_2 = random.randint(1,13)
44+
i_suite_2 = random.randint(1,4)
4045

41-
print ( f'Your second card {i_card_2} is a {a_cards[i_card_2]} of {a_suite[i_suite_2]}')
46+
print ( f'Your second card {i_card_2} is a {a_cards[i_card_2]} of {a_suite[i_suite_2]}')
4247

43-
if your_guess=="higher" :
44-
if i_card < i_card_2 :
45-
print ( s_success )
46-
else :
47-
print ( s_failure )
48-
elif your_guess=="lower" :
49-
if i_card > i_card_2 :
50-
print ( s_success )
48+
if your_guess=="H" :
49+
if i_card < i_card_2 :
50+
print ( s_success )
51+
else :
52+
print ( s_failure )
53+
elif your_guess=="L" :
54+
if i_card > i_card_2 :
55+
print ( s_success )
56+
else :
57+
print ( s_failure )
5158
else :
52-
print ( s_failure )
53-
else :
54-
print ( s_invalid )
55-
print ( s_failure )
59+
print ( s_invalid )
60+
print ( s_failure )
61+
62+
# play again?
63+
f_cont = input( f'Do you want to play again? (Y/N) ').lower()
64+
if f_cont=="n":
65+
print ( '=' * 40 )
66+
print ( 'Thanks for playing, see you soon' )
67+
break
68+
else:
69+
print ( '-' * 40 )

Assessment/Wk4/theatre_test.docx

64.8 KB
Binary file not shown.

Tasks/create_dict.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# create a dictionary from user input
2+
3+
import pprint
4+
5+
emps = {}
6+
7+
while 1==1 :
8+
new_key=input( "Enter the Employee Name: (enter 0 to exit) " )
9+
if (new_key == "0"):
10+
break
11+
new_role=input( "Enter the Employee Role: (enter 0 to exit)" )
12+
if (new_role == "0"):
13+
break
14+
new_salary=input ( "Enter the Employee Salary: (enter 0 to exit) ")
15+
if (new_salary == "0"):
16+
break
17+
emps[new_key] = [new_role, new_salary]
18+
19+
pp1=pprint.PrettyPrinter( compact=False)
20+
21+
pp1.pprint ( emps )
22+
23+

Tasks/dict_games.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Dictionary tests
2+
3+
import pprint
4+
import json
5+
6+
dict = {"bob": {"cost": 1234, "nickname": "Roger"}, \
7+
"Dave" : {"cost": 5678, "nickname": "Big D" }, \
8+
"Bert" : {"cost": 9922, "nickname": "DangerB"}, \
9+
"Sandra": {"cost": "12.55", "nickname:": "Triple S"}}
10+
11+
test = "bob"
12+
13+
print ( " Testing Print / Dict ")
14+
pp1 = pprint.PrettyPrinter(indent=2, width=50, compact=False )
15+
pp1.pprint( dict )
16+
17+
18+
19+
print ( "Testing for " + test)
20+
21+
if test in dict.keys() :
22+
print ( f"Found {test}" )
23+
else :
24+
print ( f'NOT found {test}' )

Tasks/print_list_dict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## print this data
2+
3+
stuff={'Ford': ['Researcher',60000], 'Sara': ['Lecturer', 50000]}
4+
5+
for keys, values in stuff.items():
6+
print ( keys )
7+
for entry in values :
8+
print ( f" {entry} " )

0 commit comments

Comments
 (0)