Advertisement
kajs54

ex1 s10

May 4th, 2022
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def verif(ch):
  2.     test=True
  3.     i=0
  4.     while test==True and i!=len(ch):
  5.         if not(ch[i].upper() in ["A","B","C","D","E","F"] or "0"<=ch[i].upper()<="9"):
  6.             test=False
  7.         i=i+1
  8.     return test==True
  9. def saisie():
  10.     while True:
  11.         ch=input("ch:")
  12.         if len(ch)>0 and verif(ch): break
  13.     return ch
  14. def convertir(ch):
  15.     s=0
  16.     if ch=="A":
  17.         s=10
  18.     elif ch=="B":
  19.         s=11
  20.     elif ch=="C":
  21.         s=12
  22.     elif ch=="D":
  23.         s=13
  24.     elif ch=="E":
  25.         s=14
  26.     elif ch=="F":
  27.         s=15
  28.     else:
  29.         s=int(ch)
  30.     return s
  31. def calcul(ch):
  32.     p=0
  33.     x=0
  34.     bi=16**(len(ch)-1)
  35.     for i in range(len(ch)):
  36.         x=bi*convertir(ch[i])
  37.         p=x+p
  38.         bi=bi//16
  39.     return p
  40. ch=saisie()
  41. print("n=",calcul(ch))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement