Relancer programme python

Fermé
Loxia - Modifié le 24 nov. 2022 à 19:34
verexi1513 Messages postés 1 Date d'inscription dimanche 27 novembre 2022 Statut Membre Dernière intervention 27 novembre 2022 - 27 nov. 2022 à 13:04

Bonjour, voici mon programme, je voudrais qu'il se relance si aucun des 'if' ne fonction donc après le 'else'  SVP j'ai besoin d'aide

import random

voiture = ["P1", "P2", "P3"]
des = ["P1","P2", "P3"]
choix = ["garde", "change"]
P=[]
V=(random.choice(voiture))
D=(random.choice(des))
C=(random.choice(choix))
for i in range(P!=V and P!=D):
  P=(random.choice(voiture))
  if P!=V and P!=D:
    print("Test")
    if V==D and C=="garde":
      print("Victoire")
      print("Porte choisie : ", D)
      print("Porte Présentateur", P)
      print("Porte Voiture : ", V)
      print("Votre choix : ", C)
    if V==D and C=="change":
      print("Dommage")
      print("Porte choisie : ", D)
      print("Porte Présentateur", P)
      print("Porte Voiture : ", V)
      print("Votre choix : ", C)
    if V!=D and C=="change":
      print("Victoire")
      print("Porte choisie : ", D)
      print("Porte Présentateur", P)
      print("Porte Voiture : ", V)
      print("Votre choix : ", C)
    if V!=D and C=="garde":
      print("Dommage")
      print("Porte choisie : ", D)
      print("Porte Présentateur", P)
      print("Porte Voiture : ", V)
      print("Votre choix : ", C)
  else:
    print("relance programme")

3 réponses

yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476
27 nov. 2022 à 12:57

Je serais très surpris que la ligne 10 fasse ce que tu imagines.

1
jee pee Messages postés 39632 Date d'inscription mercredi 2 mai 2007 Statut Modérateur Dernière intervention 24 avril 2024 9 234
24 nov. 2022 à 20:43

Bonjour,

Tu pourrais utiliser une boucle while,

import random
import time

voiture = ["P1", "P2", "P3"]
des = ["P1","P2", "P3"]
choix = ["garde", "change"]
while True:
   P=[]
   V=(random.choice(voiture))
   D=(random.choice(des))
   C=(random.choice(choix))
   for i in range(P!=V and P!=D):
     P=(random.choice(voiture))
     if P!=V and P!=D:
       print("Test")
       if V==D and C=="garde":
          print("Victoire")
          print("Porte choisie : ", D)
          print("Porte Présentateur", P)
          print("Porte Voiture : ", V)
          print("Votre choix : ", C)
       if V==D and C=="change":
          print("Dommage")
          print("Porte choisie : ", D)
          print("Porte Présentateur", P)
          print("Porte Voiture : ", V)
          print("Votre choix : ", C)
       if V!=D and C=="change":
          print("Victoire")
          print("Porte choisie : ", D)
          print("Porte Présentateur", P)
          print("Porte Voiture : ", V)
          print("Votre choix : ", C)
       if V!=D and C=="garde":
          print("Dommage")
          print("Porte choisie : ", D)
          print("Porte Présentateur", P)
          print("Porte Voiture : ", V)
          print("Votre choix : ", C)
     else:
        print("************relance programme")
        time.sleep(2)

0

Bonjour, merci beaucoup! ? :)

0
yg_be Messages postés 22720 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476
27 nov. 2022 à 12:55

Cette boucle infinie recommence même si on ne passe pas par "relance programme".
Ceci ne recommence que si on passe par "relance programme":

import random
encore=True
while encore:
    voiture = ["P1", "P2", "P3"]
    des = ["P1","P2", "P3"]
    choix = ["garde", "change"]
    P=[]
    V=(random.choice(voiture))
    D=(random.choice(des))
    C=(random.choice(choix))
    for i in range(P!=V and P!=D):
        P=(random.choice(voiture))
        if P!=V and P!=D:
            print("Test")
            if V==D and C=="garde":
                print("Victoire")
                print("Porte choisie : ", D)
                print("Porte Présentateur", P)
                print("Porte Voiture : ", V)
                print("Votre choix : ", C)
            if V==D and C=="change":
                print("Dommage")
                print("Porte choisie : ", D)
                print("Porte Présentateur", P)
                print("Porte Voiture : ", V)
                print("Votre choix : ", C)
            if V!=D and C=="change":
                print("Victoire")
                print("Porte choisie : ", D)
                print("Porte Présentateur", P)
                print("Porte Voiture : ", V)
                print("Votre choix : ", C)
            if V!=D and C=="garde":
                print("Dommage")
                print("Porte choisie : ", D)
                print("Porte Présentateur", P)
                print("Porte Voiture : ", V)
                print("Votre choix : ", C)
            encore=False
        else:
            print("relance programme")
0
verexi1513 Messages postés 1 Date d'inscription dimanche 27 novembre 2022 Statut Membre Dernière intervention 27 novembre 2022
27 nov. 2022 à 13:04

Slimmestar Je pense que l'utilisation d'une boucle while devrait résoudre le problème.

0