Update code comments and labels in exercise 3_6.py, add password generation functionality in exercise 4_4.py, and implement card drawing functionality in exercise 4_5.py

This commit is contained in:
Tutur33
2023-12-18 21:37:30 +01:00
parent 3d8d15b843
commit 24dd88b618
3 changed files with 87 additions and 7 deletions
+40 -1
View File
@@ -1,3 +1,11 @@
# A l'aide de la fonction randint() du module random, écrire une fonction qui retourne un mot de passe de longueur N (chiffres, lettres minuscules ou majuscules).
# On donne :
# chaine = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
# >>> print(password(10))
# mHVeC5rs8P
# >>> print(password(6))
# PYthoN
chaine = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
from random import randint, choice
@@ -16,4 +24,35 @@ def password2(n:int) -> str:
r += choice(chaine)
return r
print(password2(10))
print(password2(10))
# Avec une interface graphique :
from tkinter import *
from random import randint, choice
def password(n:int) -> str:
r = ''
for i in range(n):
r += choice(chaine)
return r
def update_label(*args):
label.config(text=f"Votre mot de passe : {password(n.get())}")
fen = Tk()
fen.title("Mot de passe")
fen.geometry("400x200")
n = IntVar()
n.set(8)
Label(fen, text="Longueur du mot de passe :").pack()
Entry(fen, textvariable=n).pack()
label = Label(fen, text=f"Votre mot de passe : {password(n.get())}")
label.pack()
Button(fen, text="Générer", command=update_label).pack()
fen.mainloop()
+38 -1
View File
@@ -1,3 +1,8 @@
# Ecrire une fonction qui retourne une carte (au hasard) d'un jeu de Poker à 52 cartes.
# On utilisera la fonction choice() ou randint() du module random.
# On donne :
# ListeCarte = ['2s','2h','2d','2c','3s','3h','3d','3c','4s','4h','4d','4c','5s','5h','5d','5c', '6s','6h','6d','6c','7s','7h','7d','7c','8s','8h','8d','8c','9s','9h','9d','9c', 'Ts','Th','Td','Tc','Js','Jh','Jd','Jc','Qs','Qh','Qd','Qc','Ks','Kh','Kd','Kc','As','Ah','Ad','Ac']
from random import choice
ListeCarte = ['2s','2h','2d','2c','3s','3h','3d','3c','4s','4h','4d','4c','5s','5h','5d','5c', '6s','6h','6d','6c','7s','7h','7d','7c','8s','8h','8d','8c','9s','9h','9d','9c', 'Ts','Th','Td','Tc','Js','Jh','Jd','Jc','Qs','Qh','Qd','Qc','Ks','Kh','Kd','Kc','As','Ah','Ad','Ac']
@@ -5,4 +10,36 @@ ListeCarte = ['2s','2h','2d','2c','3s','3h','3d','3c','4s','4h','4d','4c','5s','
def tiragecarte():
return choice(ListeCarte)
print(tiragecarte())
print(tiragecarte())
# Avec une interface graphique (Tkinter et une API) :
import requests
from tkinter import *
from PIL import Image, ImageTk
def tiragecarte():
response = requests.get("https://deckofcardsapi.com/api/deck/new/draw/?count=1")
if response.status_code == 200:
data = response.json()
return data['cards'][0]['image']
return None
def affichecarte():
image_url = tiragecarte()
if image_url:
print(image_url)
img = Image.open(requests.get(image_url, stream=True).raw)
img = img.resize((100, 150), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(img)
canvas.image = photo
canvas.create_image(50, 75, image=photo)
else:
print("Erreur lors du tirage de la carte")
fenetre = Tk()
canvas = Canvas(fenetre, width=100, height=150, bg="ivory")
canvas.pack()
bouton = Button(fenetre, text="Tirer une carte", command=affichecarte)
bouton.pack()
fenetre.mainloop()