-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.py
More file actions
49 lines (38 loc) · 1.63 KB
/
coin.py
File metadata and controls
49 lines (38 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# while True:
# user_input = input("Throw the coin and enter head or tail here: ")
# filename = 'files/coin_input.txt'
# match user_input:
# case 'head' | 'tail':
# with open(filename, 'r') as file:
# file_contents = file.readlines()
# with open(filename, 'w') as file:
# file.writelines(file_contents)
# file.write(f"{user_input}\n")
# with open(filename, 'r') as file:
# file_contents = file.readlines()
# file_contents = [file_content.strip('\n') for file_content in file_contents]
# head_count = file_contents.count('head')
# total_count = len(file_contents)
# head_percent = float(100*head_count/total_count)
# print(f"Heads: {head_percent}%")
# case 'exit':
# break
##############
while True:
user_input = input("Throw the coin and enter head or tail here: ")
filename = '../files/coin_input.txt'
match user_input:
case 'head' | 'tail':
with open(filename, 'r') as file:
file_contents = file.readlines()
file_contents.append(f"{user_input}\n")
with open(filename, 'w') as file:
file.writelines(file_contents)
file_contents = [file_content.strip('\n') for file_content in file_contents]
head_count = file_contents.count('head')
total_count = len(file_contents)
head_percent = float(100*head_count/total_count)
print(f"Heads: {head_percent}%")
case 'exit':
break
################