Skip to content
Snippets Groups Projects
Commit 4235f137 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

TP1 terminé

parents
Branches
No related tags found
No related merge requests found
/.idea
\ No newline at end of file
main.py 0 → 100644
import requests
def get_posts():
posts = requests.get('https://jsonplaceholder.typicode.com/posts')
return posts.json()[:5]
def get_users():
users = requests.get('https://jsonplaceholder.typicode.com/users')
return users.json()[:5]
def create_post(title, body):
posts = requests.get('https://jsonplaceholder.typicode.com/posts')
last_id = posts.json()[-1]['id']
new_post = {
'userId': 1,
'id': last_id + 1,
'title': title,
'body': body,
}
requests.post('https://jsonplaceholder.typicode.com/posts', json=new_post)
return last_id + 1
def main():
print("Souhaitez-vous:")
print("1. Lister les 5 premiers posts")
print("2. Lister les 5 premiers utilisateurs")
print("3. Créer un post")
choice = input("Votre choix: ")
if choice == "1":
print("Liste des 5 premiers posts:")
posts = get_posts()
for post in posts:
print("Nom: " + post['title'])
print("Contenu: " + post['body'])
print("")
elif choice == "2":
users = get_users()
usersString = ""
for user in users:
usersString += user['name'] + ", "
print("Liste des 5 premiers utilisateurs: ")
print(usersString[:-2])
elif choice == "3":
title = input("Titre: ")
body = input("Contenu: ")
new_post_id = create_post(title, body)
print("Le post a été créé avec l'id " + str(new_post_id))
else:
print("Choix invalide")
if __name__ == "__main__":
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment