crypto.py

Created by schraf

Created on October 12, 2021

2.22 KB


from random import randint
from math import sin, radians

class monnaie:
 def __init__(self,dx,a,b):
  self.pos = 0
  self.dx = dx
  self.a = a
  self.b = b
 def maj(self):
  self.histo = self.prix()
  self.pos += randint(-self.dx,self.dx)
 def prix(self):
  return self.a * sin(radians(self.pos)) + self.b 

class joueur:
 def __init__(self,bk = 1000,pf = {}):
  self.bk = bk
  self.pf = pf
 def achatVente(self,montant,crypto,cm):
  try:
    if montant == 0:
      print("Vous attendez")
      return
    if 0 < montant <= self.bk:
      if crypto in self.pf.keys():
        self.pf[crypto] += montant / cm[crypto].prix()
      else:
        self.pf[crypto] = montant / cm[crypto].prix()
      self.bk -= montant
      return
    if montant < 0 and crypto in self.pf.keys():
      if self.pf[crypto] * cm[crypto].prix() >= abs(montant):
        self.pf[crypto] += montant / cm[crypto].prix()
        self.bk -= montant
        return
  except: pass
  print("Impossible !")

def montantPf(joueur,cm):
  s = joueur.bk
  for nom in joueur.pf.keys():
    s += joueur.pf[nom] * cm[nom].prix()
  return s  

def majMarche(cm,jour):
  print("--- JOUR {} ---".format(jour)) 
  for nom in cm.keys(): 
    cm[nom].maj()
    print('{}:{:.2f} ({:.2f})'.format(nom,cm[nom].prix(),cm[nom].histo))

def demandeAction():
    c = None
    v = input("Montant : ")
    try: v = float(v)
    except ValueError: v = 0
    if v != 0:
      c = input("Quel crypto ? : ")
    return [v,c]

def affInfos(joueur,cm):
  print("-- PORTEFEUILLE --") 
  print("Banque: {:.2f}".format(joueur.bk))
  for nom in joueur.pf.keys():
    print("{}: {:.2f}".format(nom,joueur.pf[nom] * cm[nom].prix()))
  print("Total: {:.2f}".format(montantPf(joueur,cm)))

def msgFinal(joueur, cm):
  total = montantPf(joueur,cm)
  print('Total (EUR): {}'.format(total))

def initCm(noms):
  marche = {}
  for nom in noms:
    dx = randint(5,40)
    a = randint(2,6)
    b = a + randint(1, 5)
    marche[nom] = monnaie(dx,a,b)
  return marche

nomsCm = ['btc','bat']

def jeu():
  jour = 1
  j1 = joueur()
  cm = initCm(nomsCm)
  while jour < 31:
    majMarche(cm,jour)
    affInfos(j1,cm)
    [montant,crypto] = demandeAction()
    j1.achatVente(montant,crypto,cm)
    jour += 1
  msgFinal(j1, cm)

jeu()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.