Problème avec une valeur suite à l'utilisation d'une boucle

Fermé
arthurlfbv - 3 nov. 2022 à 14:37
Phil_1857 Messages postés 1883 Date d'inscription lundi 23 mars 2020 Statut Membre Dernière intervention 28 février 2024 - 4 nov. 2022 à 12:28

Bonjour,

J'ai un problème avec une valeur qui n'est pas la même a l'intérieur qu'en dehors de la boucle 

voici mon script:

nom_pl1='messi'
nom_pl2='ronaldo'

point_pl1= 0
point_pl2= 0

jeu_pl1= 0
jeu_pl2= 0

set_pl1= 0
set_pl2= 0

while point_pl1 !=4 or point_pl2 !=4:
    from random import randint
    a = randint(0,1)
    if a == 0:
        point_pl1 += 0
        point_pl2 += 1
    else:
        point_pl1 +=1
        point_pl2 +=0
    if point_pl1 >4:
        break
    if point_pl2 >4:
        break
    print(f"{point_pl1} {nom_pl1}")
    print(f"{point_pl2} {nom_pl2}")
    from random import randint
    b = randint(0,1)
    if b == 0:
        point_pl1 += 0
        point_pl2 += 1
    else:
        point_pl1 +=1
        point_pl2 +=0
    if point_pl1 >4:
        break
    if point_pl2 >4:
        break
    print(f"{point_pl1} {nom_pl1}")
    print(f"{point_pl2} {nom_pl2}")
    from random import randint
    c = randint(0,1)
    if c == 0:
        point_pl1 += 0
        point_pl2 += 1
    else:
        point_pl1 +=1
        point_pl2 +=0
    if point_pl1 >4:
        break
    if point_pl2 >4:
        break
    print(f"{point_pl1} {nom_pl1}")
    print(f"{point_pl2} {nom_pl2}")
    from random import randint
    d = randint(0,1)
    if d == 0:
        point_pl1 += 0
        point_pl2 += 1
    else:
        point_pl1 +=1
        point_pl2 +=0
    if point_pl1 >4:
        break
    if point_pl2 >4:
        break
    if point_pl1 ==4 and point_pl2 !=0:
        break
    if point_pl2 ==4 and point_pl1 !=0:
        break
    if point_pl1 >4:
        break
    if point_pl2 >4:
        break
    print(f"{point_pl1} {nom_pl1}")
    print(f"{point_pl2} {nom_pl2}")
print(f"{point_pl1}")
print(f"{point_pl2}")    
    

Windows / Chrome 107.0.0.0

3 réponses

Phil_1857 Messages postés 1883 Date d'inscription lundi 23 mars 2020 Statut Membre Dernière intervention 28 février 2024 178
Modifié le 3 nov. 2022 à 14:55

Bonjour,

on y verrait plus clair si tu affichais ton code avec les balises de code
mode d'emploi:

https://codes-sources.commentcamarche.net/faq/11288-poster-un-extrait-de-code

Visuellement, ça doit ressembler à ça:

for k in range(10):
    print(k)

C'est normal:

tu affiches dans la boucle point_pl1 qui est égal à 4

ensuite tu refais un tour de boucle en incrémentant point_pl1, qui passe donc à 5 et comme il est > 4 : break,

on sort de la boucle sans repasser sur les print 

donc après la boucle point_pl1 = 5 ! CQFD

0

Salut,

Un import ce fait en début de script, donc inutile de le répéter 2 fois dans une boucle while

0
Phil_1857 Messages postés 1883 Date d'inscription lundi 23 mars 2020 Statut Membre Dernière intervention 28 février 2024 178
4 nov. 2022 à 12:28

C'est vrai les imports se font au début

Mais ça ne change rien au fait que point_p1 est à 5 en sortie de boucle

Ce serait sympa qu'Arthur nous réponde 

0