Skip to content

Commit 603defe

Browse files
author
Mark Charlton
committed
After wk 12 and playing
1 parent 826ca6a commit 603defe

File tree

6 files changed

+255
-77
lines changed

6 files changed

+255
-77
lines changed

Assessment/Wk11/hangman.jpg

31.7 KB
Loading

Assessment/Wk11/hangman.py

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,127 @@
22

33
# params
44
api_ninjsas_key = "NZ7cUijXf6uoivDW6yl/EA==ufxyi04CTS0N25V6"
5+
i_lives = 6
6+
i_lives_remaining = i_lives
7+
s_blank = "-"
8+
i_sep_len = 50
9+
10+
# Variables
11+
a_guesses=[]
12+
a_letters=[]
513

614
import requests
715
import json
16+
import sys
17+
import os
18+
import time
19+
20+
sys.path.append(os.path.abspath('..\\inc\\'))
21+
22+
import screen as s
23+
import helpers as h
824

925
def get_word():
1026
api_url = 'https://api.api-ninjas.com/v1/randomword'
1127
response = requests.get(api_url, headers={'X-Api-Key': api_ninjsas_key})
1228
if response.status_code == requests.codes.ok:
1329
a_ret = json.loads(response.text)
14-
return a_ret['word']
30+
return a_ret['word'].upper()
1531
else:
1632
print("Error:", response.status_code, response.text)
1733
return ""
1834

35+
def remove_dups(x):
36+
return list(dict.fromkeys(x))
37+
38+
def show_grid():
39+
print ( s.RESET )
40+
print ( "Hangman Beta v0.1 ")
41+
print ( "Words courtesy of api-ninjas.com Random Words API")
42+
print ( "=" * i_sep_len)
43+
print ( "" )
44+
print ( f"Lives Remaining: {i_lives_remaining}/{i_lives} --> [ ", s.GREENON, "0" * (i_lives_remaining), s.REDON, "X" * (i_lives-i_lives_remaining), s.COLOUROFF, "]")
45+
print ( "" )
46+
print ( f"The word has {i_len} characters")
47+
print ( "" )
48+
print ( "=" * i_sep_len)
49+
print ( "" )
50+
print ( "The Word: ", end ="" )
51+
for i in range(i_len):
52+
if s_word[i] in a_guesses:
53+
print ( s_word[i], end = "" )
54+
else:
55+
print ( s_blank , end = "" )
56+
print ( "" )
57+
print ( "" )
58+
print ( "=" * i_sep_len)
59+
print ( "" )
60+
print ( "Your Guesses: ", end ="" )
61+
print (", ".join(a_guesses) )
62+
print ( "" )
63+
print ( "=" * i_sep_len)
64+
print ( "" )
65+
66+
def check_won():
67+
f_win = True
68+
for val in a_letters:
69+
if val not in a_guesses:
70+
f_win = False
71+
break
72+
73+
return f_win
74+
75+
def exit_print():
76+
print ( "Please play again soon " )
77+
print ( "" )
78+
print ( "The word was: " + s_word)
79+
print ( "" )
80+
81+
1982
s_word = get_word()
2083
if s_word != "":
21-
print ( f"The word is '{s_word}'" )
84+
#print ( f"The word is '{s_word}'" )
85+
i_len = len(s_word)
86+
a_letters = remove_dups([*s_word])
87+
a_letters.sort()
88+
print (a_letters)
89+
show_grid()
90+
91+
while check_won()==False:
92+
s_guess = input ( "Please enter a letter (type EXIT to quit): ").upper()
93+
if s_guess == "EXIT":
94+
exit_print()
95+
break
96+
97+
# make sure it's only one character.
98+
s_guess=s_guess[0]
99+
100+
if s_guess in a_guesses:
101+
print ( "You've already guessed that!" )
102+
103+
elif s_guess in a_letters:
104+
print ( "Yey, you've guessed correctly ")
105+
a_guesses.append(s_guess)
106+
a_guesses.sort()
107+
108+
else:
109+
print ( "Oh No, that's not in the word")
110+
a_guesses.append(s_guess)
111+
a_guesses.sort()
112+
113+
i_lives_remaining -= 1
114+
if i_lives_remaining < 1:
115+
print ( "All lives lost! :( " )
116+
exit_print()
117+
break
118+
119+
time.sleep(1)
120+
show_grid()
121+
122+
if check_won():
123+
print ( "*" * i_sep_len )
124+
print ( f"{'Congratulations':^{i_sep_len}}")
125+
print ( "*" * i_sep_len )
126+
print ( "" )
127+
128+

Assessment/Wk4/theatre.py

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ def get_y_n ( input_prompt ):
4444
return return_value
4545

4646

47-
4847
# inits
4948
costs = { 'Adult' : 10.50 , 'Child' : 7.30 , 'Conc' : 8.40 }
5049
num_kids_for_free_adult = 10
51-
5250
discount = { 'minimum_spend': float(100.00), 'discount' : int(10) }
53-
5451
the_p_n_p_cost = float(2.34)
5552

5653
# welcome
@@ -82,74 +79,83 @@ def get_y_n ( input_prompt ):
8279
num_paying_adults=num_adults-num_free_adults
8380
num_paying_conc=num_conc-num_free_conc
8481

85-
ticket_values={"Adult": 0, "Conc": 0 , "Child": 0}
86-
ticket_values["Adult"] = num_paying_adults * costs["Adult"]
87-
ticket_values["Conc"] = num_paying_conc * costs["Conc"]
88-
ticket_values["Child"] = num_kids * costs["Child"]
89-
90-
ttl_bill_pre_discount = ( ticket_values["Adult"] ) + \
91-
( ticket_values["Conc"] ) + \
92-
( ticket_values["Child"] )
93-
94-
# calculate discount
95-
discount_percentage = 0
96-
bill_discount = 0
97-
if ttl_bill_pre_discount > discount["minimum_spend"] :
98-
discount_percentage=discount["discount"]
99-
bill_discount = ttl_bill_pre_discount * ( discount_percentage / 100 )
100-
101-
# calculate delivery
102-
103-
if is_collection == "Y" :
104-
p_n_p_cost=0
105-
else :
106-
p_n_p_cost=the_p_n_p_cost
107-
108-
bill_total=ttl_bill_pre_discount - bill_discount + p_n_p_cost
109-
110-
# generate bill reference
111-
bill_ref=str(time.time())[3:10]
112-
113-
# output bill
114-
print ( )
115-
print ( "The Sandford Paladium")
116-
print ( "*" * 21 )
117-
print ()
118-
119-
print ( f'{num_in_party} Tickets for Snakes! The Musical' )
120-
print ( f'{"Order Ref: " + bill_ref:>36}')
121-
print ( '-' * 36 )
122-
if is_collection == "Y" :
123-
print ( "*" * 9, " COLLECTION ", "*" * 9 )
82+
# check they have adults to cover the kids
83+
if num_kids == num_in_party:
84+
print ("Invalid order. All children must have at least one adult attending.")
85+
print ("Please try to place your order again. ")
86+
else:
87+
# output the receipt.
88+
ticket_values={"Adult": 0, "Conc": 0 , "Child": 0}
89+
ticket_values["Adult"] = num_paying_adults * costs["Adult"]
90+
ticket_values["Conc"] = num_paying_conc * costs["Conc"]
91+
ticket_values["Child"] = num_kids * costs["Child"]
92+
93+
ttl_bill_pre_discount = ( ticket_values["Adult"] ) + \
94+
( ticket_values["Conc"] ) + \
95+
( ticket_values["Child"] )
96+
97+
# calculate discount
98+
discount_percentage = 0
99+
bill_discount = 0
100+
if ttl_bill_pre_discount > discount["minimum_spend"] :
101+
discount_percentage=discount["discount"]
102+
bill_discount = ttl_bill_pre_discount * ( discount_percentage / 100 )
103+
104+
# calculate delivery
105+
106+
if is_collection == "Y" :
107+
p_n_p_cost=0
108+
else :
109+
p_n_p_cost=the_p_n_p_cost
110+
111+
bill_total=ttl_bill_pre_discount - bill_discount + p_n_p_cost
112+
113+
# generate bill reference
114+
bill_ref=str(time.time())[3:10]
115+
116+
# output bill
117+
print ( )
118+
print ( "The Sandford Paladium")
119+
print ( "*" * 21 )
120+
print ()
121+
122+
print ( f'{num_in_party} Tickets for Snakes! The Musical' )
123+
print ( f'{"Order Ref: " + bill_ref:>36}')
124124
print ( '-' * 36 )
125-
126-
if num_adults > 0:
127-
print ( f'{num_adults:3} {"Adult":12} ' )
128-
if num_paying_adults > 0 : print ( ' ' * 12 , f'{num_paying_adults:3} @ £{costs["Adult"]:5.2f} = £{ ticket_values["Adult"]:7.2f} ')
129-
if num_free_adults > 0 : print ( ' ' * 12 , f'{num_free_adults:3} @ £{0:5.2f} = £{0:7.2f} ')
130-
print ( "" )
131-
132-
if num_conc > 0:
133-
print ( f'{num_conc:3} {"Concessions":12} ' )
134-
if num_paying_conc > 0 : print ( ' ' * 12 , f'{num_paying_conc:3} @ £{costs["Conc"]:5.2f} = £{ ticket_values["Conc"]:7.2f} ')
135-
if num_free_conc > 0 : print ( ' ' * 12 , f'{num_free_conc:3} @ £{0:5.2f} = £{ 0:7.2f} ')
136-
print ( "" )
137-
138-
if num_kids > 0 :
139-
print ( f'{num_kids:3} {"Child":12} @ £{costs["Child"]:5.2f} = £{ ticket_values["Child"]:7.2f} ')
140-
if num_kids > 9 : print ( f" (Includes {num_free_adults+num_free_conc} free chaperones)")
141-
print ( "" )
142-
143-
print ( ' ' * 27, '=' * 8 )
144-
145-
if discount_percentage > 0 :
146-
print ( ' ' * 9 , f'{"Sub Total":17} £{ttl_bill_pre_discount:7.2f}' )
147-
print ( ' ' * 9 , f'{"Discount (" + str(discount["discount"]) + "%)":17} £{bill_discount:7.2f}' )
148-
print ( ' ' * 27, '-' * 8 )
149-
150-
if p_n_p_cost > 0 :
151-
print ( ' ' * 9 , f'{"Postage":17} £{p_n_p_cost:7.2f}' )
152-
print ( ' ' * 27, '-' * 8 )
153-
154-
print ( ' ' * 9 , f'{"Total":17} £{bill_total:7.2f}' )
155-
print ( ' ' * 27, '=' * 8 )
125+
if is_collection == "Y" :
126+
print ( "*" * 9, " COLLECTION ", "*" * 9 )
127+
print ( '-' * 36 )
128+
129+
if num_adults > 0:
130+
print ( f'{num_adults:3} {"Adult":12} ' )
131+
if num_paying_adults > 0 : print ( ' ' * 12 , f'{num_paying_adults:3} @ £{costs["Adult"]:5.2f} = £{ ticket_values["Adult"]:7.2f} ')
132+
if num_free_adults > 0 : print ( ' ' * 12 , f'{num_free_adults:3} @ £{0:5.2f} = £{0:7.2f} ')
133+
print ( "" )
134+
135+
if num_conc > 0:
136+
print ( f'{num_conc:3} {"Concessions":12} ' )
137+
if num_paying_conc > 0 : print ( ' ' * 12 , f'{num_paying_conc:3} @ £{costs["Conc"]:5.2f} = £{ ticket_values["Conc"]:7.2f} ')
138+
if num_free_conc > 0 : print ( ' ' * 12 , f'{num_free_conc:3} @ £{0:5.2f} = £{ 0:7.2f} ')
139+
print ( "" )
140+
141+
if num_kids > 0 :
142+
print ( f'{num_kids:3} {"Child":12} @ £{costs["Child"]:5.2f} = £{ ticket_values["Child"]:7.2f} ')
143+
s_plural=""
144+
if num_free_adults+num_free_conc > 1:
145+
s_plural="s"
146+
if num_kids > 9 : print ( f" (Includes {num_free_adults+num_free_conc} free chaperone{s_plural})")
147+
print ( "" )
148+
149+
print ( ' ' * 27, '=' * 8 )
150+
151+
if discount_percentage > 0 :
152+
print ( ' ' * 9 , f'{"Sub Total":17} £{ttl_bill_pre_discount:7.2f}' )
153+
print ( ' ' * 9 , f'{"Discount (" + str(discount["discount"]) + "%)":17} £{bill_discount:7.2f}' )
154+
print ( ' ' * 27, '-' * 8 )
155+
156+
if p_n_p_cost > 0 :
157+
print ( ' ' * 9 , f'{"Postage":17} £{p_n_p_cost:7.2f}' )
158+
print ( ' ' * 27, '-' * 8 )
159+
160+
print ( ' ' * 9 , f'{"Total":17} £{bill_total:7.2f}' )
161+
print ( ' ' * 27, '=' * 8 )

Assessment/Wk6/rainbow.jpg

75.5 KB
Loading

Assessment/Wk6/rainbow.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,44 @@
22

33
# inits
44
a_rb = ['Rainbow Colours','Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']
5+
a_scb = ["0;0;0m","255;0;0m", "255;165;0m", "255;255;0m", "0;255;0m", "0;0;255m", "255;85;255m", "170;0;170m"]
6+
a_scf = ["255;255;255m","255;255;255m", "0;0;0m", "0;0;0m", "0;0;0m", "255;255;255m", "255;255;255m", "255;255;255m"]
7+
8+
s_fg = "\033[38;2;"
9+
s_bg = "\033[48;2;"
10+
11+
# this prints the text with the right background
12+
def print_colour(i):
13+
s_col_on=s_fg + a_scf[i]
14+
s_col_off=s_fg + a_scf[0]
15+
s_bg_on=s_bg + a_scb[i]
16+
s_bg_off=s_bg + a_scb[0]
17+
18+
print ( f"{s_bg_off}{s_col_off}{i}:{s_col_on}{s_bg_on} {a_rb[i]:^10} {s_bg_off}{s_col_off}")
19+
pass
520

621
while True:
722
i_val = int ( input ( "Enter a value between 1 and 7 : (-1 to exit)") )
23+
24+
# exit condition
825
if i_val == int(-1):
926
break
1027

28+
# required output condition
1129
if i_val > 0 and i_val < 8 :
12-
print ( f"{i_val}: {a_rb[i_val]}")
13-
30+
print_colour ( i_val )
31+
32+
# this is not in the assignment but I added it to ease debugging with the colour combinations
33+
elif i_val == 0:
34+
print ( "You found the Easter Egg. \nA Double Rainbow :)")
35+
36+
for i in range(7,0,-1):
37+
print_colour ( i )
38+
print ("")
39+
for i in range(1,8):
40+
print_colour ( i )
41+
42+
# error message
43+
else:
44+
print ( "Invalid option selected")
45+

Tasks/scratch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# my code
3+
4+
arr = [["J" for i in range(4)],
5+
["Q" for i in range(4)],
6+
["K" for i in range(4)],
7+
["A" for i in range(4)],]
8+
9+
# my print
10+
11+
print (" 1 2 3 4")
12+
for i in range(4):
13+
print ( str(i+1), end= " " )
14+
for j in range(4):
15+
print ( "#", end=" " )
16+
#print ( arr[i][j], end=" " )
17+
print ( "" )
18+
19+
print ( "" )
20+
21+
print (" 1 2 3 4")
22+
for i in range(4):
23+
print ( str(i+1), end= " " )
24+
for j in range(4):
25+
#print ( "#", end=" " )
26+
print ( arr[i][j], end=" " )
27+
print ( "" )
28+
29+
30+
31+
# a,b = "1 2 3 4".split()
32+
# print ( a )
33+
# print ( b )

0 commit comments

Comments
 (0)