-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharry_potter.py
More file actions
49 lines (42 loc) 路 1.41 KB
/
Copy pathharry_potter.py
File metadata and controls
49 lines (42 loc) 路 1.41 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
import requests
import json
import random
def random_hp_character():
# Save the content of the API in a variable called 'r'
r = requests.get('https://hp-api.herokuapp.com/api/characters')
# Save the json content of the variable r in a variable called response_dict
response_dict = r.json()
# Obtain the total number of characters
total_caracters = len(response_dict)
# Obtain a random number from 0 to total_caracters
random_number = random.randint(0, total_caracters-1)
# Extract one of the characters
character = response_dict[random_number]
# Extract the information requiered
name = character['name']
gender = character['gender']
if gender == 'male':
he_she = 'He'
his_her = 'His'
elif gender == 'female':
he_she = 'She'
his_her = 'Her'
else:
he_she = 'It'
him_her = 'Its'
wizard = character['wizard']
if wizard:
wizard_text = ' is a wizard. '
else:
wizard_text = ' is not a wizard. '
house = character['house']
if house == "":
house_text = ' does not belong to a wizard house. '
else:
house_text = f' belongs to {house} house. '
picture = character['image']
if picture == "":
picture_text = 'Does not have a picture available right now.'
else:
picture_text = picture
return name, he_she, his_her, wizard_text, house_text, picture_text