-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Assignment 2.py
More file actions
213 lines (153 loc) · 6.2 KB
/
Python_Assignment 2.py
File metadata and controls
213 lines (153 loc) · 6.2 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Create a console app that interacts with an API -
# it gets some data and does a meaningful transformation /
# use boolean values and if else statements to branch logic to your program //
# use a data structure: list/ dictionary/ tuple or set to store values /
# use a for or while loop to reduce repetition /
# use string slicing /
# use at least TWO inbuilt functions //
# print() / help() type() str() float() int() / ord() chr() len() /
# use any free API to get some information as json /
# import an additional module and use it /
# Access the API
import requests
import time
import random
endpoint = "https://api.thedogapi.com/v1/breeds";
api_key = "live_zMs6ctnpN7sHIuAPBJnKy18S6FSochsksGfW7yvtMpgGKdoTeUTqDfvOVd8Tnghs"
response = requests.get(endpoint)
data = response.json()
# Get the list of dogs available within the API
def get_dog_breeds():
url = "https://api.thedogapi.com/v1/breeds"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print("Failed to fetch dog breeds.")
return []
# Saves the breed name and temperament to a txt file
filename1 = 'Random_dogs.txt'
def save_breed_data(breeds, filename1):
with open(filename1, "a+") as file:
for breed in breeds:
file.write(f"Name: {breed['name']}\n")
file.write(f"Temperament: {breed['temperament']}\n")
file.write("-" * 40 + "\n")
# Saves the people and favourite dog breed to a txt file
filename2 = "Fave_dogs.txt"
def save_people_data(people, filename2):
with open(filename2, "w") as file:
for person, breed in people.items():
file.write(f"Name: {person}\n")
file.write(f"Favourite Dog Breed: {breed}\n")
file.write("-" * 40 + "\n")
## ! FOR THE OPTION SELECTION ! ##
# Random Dog breed time!
def random_breed():
dog_breeds = get_dog_breeds()
chosen_breed = random.choice(dog_breeds)
print(f"\nYou got a random breed: {chosen_breed['name']}\n")
chosen_breeds = [chosen_breed]
save_breed_data(chosen_breeds, filename1)
print(f"Breed information saved to ", filename1)
# Favourite Dog Breed
favourite_dog_breeds = {}
def fave_dog():
person_name = input("Enter name: ")
favourite_breed = input("What is their favourite dog breed? ")
favourite_dog_breeds[person_name] = favourite_breed
print(f"{person_name}'s favourite dog breed, {favourite_breed}, has been recorded.")
save_people_data(favourite_dog_breeds, "Fave_dogs.txt")
# View favourite dog breeds
def view_favourite_dog_breeds():
print("Favorite Dog Breeds:")
for person, breed in favourite_dog_breeds.items():
short_name = person[:3]
print(f"{short_name}: {breed}")
# Find dogs saved in txt file
def find_dog_breed_by_name(filename2):
search_name = input("Enter the name to search for (in the Fave_dog.txt): ").lower()
try:
with open(filename2, "r") as file:
lines = file.readlines()
found = False
for i in range(0, len(lines), 3): # Process lines in sets of 3
name_line = lines[i].strip()
breed_line = lines[i + 1].strip()
if name_line.startswith("Name:") and breed_line.startswith("Favourite Dog Breed:"):
name = name_line.split(":")[1].strip().lower()
breed = breed_line.split(":")[1].strip()
if name == search_name:
print(f"Name: {name}".title())
print(f"Favourite Dog Breed: {breed}")
print("-" * 40)
found = True
if not found:
print(f"No record found for '{search_name}'.")
except FileNotFoundError:
print(f"The file '{filename2}' was not found.")
except Exception as e:
print(f"An error occurred while reading the file: {e}")
def option_4():
print("Option 4: Find Dog Breeds by Name")
find_dog_breed_by_name("Fave_dogs.txt")
# Dog Person Quiz
def dog_person_quiz():
dog_intro = input('Do you like dogs? y/n ')
is_good = dog_intro == 'y'
dog_intro2 = input('Do you have a dog? y/n ')
has_dog = dog_intro2 == 'y'
if is_good and has_dog:
print('Definitely a dog person')
if is_good and not has_dog:
print('You are a dog person')
if not is_good and not has_dog:
print('Are you a cat person?')
# Palindrome Fun
def palindrome_fun():
tacocat_word = input("What is a dog's favourite toy? ")
def isPalindrome(string):
leftIdx = 0
rightIdx = len(string) - 1
while leftIdx < rightIdx:
if string[leftIdx] != string[rightIdx]:
return False
leftIdx += 1
rightIdx -= 1
return True
print("Is {} a palindrome?: ". format(tacocat_word), isPalindrome((tacocat_word)),"(Regardless if it is a palindrome, they just love to play!")
# Option Selection
print("Welcome! Let's explore some dog breeds!")
print("Copyright 2023, All Rights Reserved by Hayley So")
def print_menu():
print("--------------------------------------------")
print("1. Get a random dog breed")
print("2. What is your favourite dog breed?")
print("3. View favourite dog breeds")
print("4. Find dog breeds from name")
print("5. Are you a dog person?")
print("6. Palindrome Fun")
print("--------------------------------------------")
while True:
print_menu()
option = int(input("Please select an option "))
if option == 1:
random_breed()
time.sleep(1.25)
elif option == 2:
fave_dog()
time.sleep(1.25)
elif option == 3:
view_favourite_dog_breeds()
time.sleep(1.25)
elif option == 4:
option_4()
time.sleep(1.25)
elif option == 5:
dog_person_quiz()
time.sleep(1.25)
elif option == 6:
palindrome_fun()
time.sleep(1.25)
else:
print("Invalid Option. Please select a number between 1 - 6")