-
Notifications
You must be signed in to change notification settings - Fork 4
/
assignment_03_02_2022.py
122 lines (101 loc) · 3.48 KB
/
assignment_03_02_2022.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 1. create a 2-D array and divide each array of both 2-D array
def create_2D_array(m, n):
return [[i for i in range(m)] for j in range(n)]
def divide_2D_array(array, m, n):
for i in range(m):
for j in range(n):
array[i][j] = i * j
return array
arr = create_2D_array(3, 3)
# print(arr)
# print(divide_2D_array(arr, 3, 3))
def create_country_with_capital():
import json
# open data.json file and read data
countries = []
with open("data.json", "r") as f:
data = json.load(f)
for country in data:
countries.append({
"name": country["name"],
"capital": country["capital"]
})
return countries
# 2. create a game using if statements and user input
def capital_city_guess(countries: list):
"""
using countries data, create a game
"""
import random
question = random.choice(countries)
answer = input(f"what is the capital city of {question['name']}?")
if answer.lower() == question["capital"].lower():
print("you are correct, the capital city is", question["capital"])
else:
print("you are wrong")
print(f"the capital city of {question['name']} is {question['capital']}\n")
# capital_city_guess(create_country_with_capital())
# 3. create a list, pop and replace an element in the list, then convert the list to a tuple
def create_list() -> list:
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def pop_list(items: list) -> int:
return items.pop()
def replace_list(items: list, value: int) -> list:
items[0] = value
return items
def to_tuple(items: list) -> tuple:
return tuple(items)
# 4. create L pattern using letter L
def create_L_pattern():
for i in range(5):
print("L" * 8 if i == 4 else "L" )
# create_L_pattern()
# 5. create a while loop that prints your name 10 times
def print_name(name: str):
i = 0
while i < 10:
print(name)
i += 1
# print_name("Iden")
# 6. Create a program that prints out prime numbers using the while loop
def print_prime_numbers(n: int) -> list:
prime_numbers = []
i = 2
while i < n:
for j in range(2, i):
if i % j == 0:
break
else:
prime_numbers.append(i)
i += 1
return prime_numbers
# print(print_prime_numbers(100))
def main():
# create a cli to run the programs
import argparse
parser = argparse.ArgumentParser(description="This is a cli to run the programs")
parser.add_argument("-p", "--program", help="the program you want to run", type=str)
parser.add_argument("-n", "--number", help="the number you want to run the program", type=int)
args = parser.parse_args()
if args.program == "prime":
print(print_prime_numbers(args.number))
elif args.program == "L":
create_L_pattern()
elif args.program == "name":
print_name("Iden")
elif args.program == "divide":
print(divide_2D_array(arr, 3, 3))
elif args.program == "capital":
capital_city_guess(create_country_with_capital())
elif args.program == "list" or args.program == "tuple" or args.program == "pop":
items = create_list()
print(items)
popped_number = pop_list(items)
print(replace_list(items, popped_number * 4))
print(to_tuple(items))
else:
# print unrecognized program and show the help
print(f"unrecognized program {args.program}")
parser.print_help()
if __name__ == "__main__":
main()