flechettes.py

Created by schraf

Created on September 18, 2018

614 Bytes

Vous devez écrire une fonction score qui calcule le score à un jeu de fléchettes sachant que l’on obtient :

  • 0 point si la fléchette est à plus de 10cm du centre
  • 5 points si la fléchette est entre 5 et 10cm (inclus) du centre
  • 10 points si la fléchette est à moins de 5cm du centre

De plus, si tous les lancers sont à moins de 5cm on gagne un bonus de 100 points.

Les différents lancers sont enregistrés dans une liste que l’on passera en paramètre à la fonction.

>> score([1, 5, 11])
15
>> score([15, 20, 30])
0
>> score([1, 2, 3, 4])
140

Explications en vidéo


def score(lancers):
  if len(lancers)==0:return 0
  score=0
  bonus=True
  for r in lancers:
    if r<5:
      score+=10
    elif r<=10:
      score+=5
      bonus=False
    else:
      bonus=False
  if bonus:
    score+=100
  return score
  
# Version 2

def scorev2(lancers):
  if len(lancers)==0:return 0
  score=0
  for r in lancers:
    score+= 10 if r<5 else 5 if r<=10 else 0
  if all(r<5 for r in lancers):
    score+=100
  return score
  
# Version 3

def scorev3(lancers):
  score=sum(map(lambda r:10 if r<5 else 5 if r<=10 else 0, lancers))
  return score+100*all(r<5 for r in lancers)

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.