You can download this code by clicking the button below.
This code is now available for download.
This code block includes three functions: fetch_random_user to fetch data from a random user API, get_random_user_info to get the name, email, and phone number of a random user, and create_user_profile to create a dictionary containing user information.
Technology Stack : Requests, json, random
Code Type : Function
Code Difficulty : Intermediate
import requests
import json
from random import choice
def fetch_random_user():
url = 'https://randomuser.me/api/'
response = requests.get(url)
return response.json()
def get_random_user_info():
user_info = fetch_random_user()
name = user_info['results'][0]['name']['title'] +. ' ' + user_info['results'][0]['name']['first'] + ' ' + user_info['results'][0]['name']['last']
email = user_info['results'][0]['email']
phone = user_info['results'][0]['phone']
return name, email, phone
def create_user_profile():
name, email, phone = get_random_user_info()
profile = {
'name': name,
'email': email,
'phone': phone
}
return profile