Vérifier un élément dans une liste
Résolu
Khbeoze
Messages postés
4
Date d'inscription
Statut
Membre
Dernière intervention
-
Khbeoze Messages postés 4 Date d'inscription Statut Membre Dernière intervention -
Khbeoze Messages postés 4 Date d'inscription Statut Membre Dernière intervention -
A voir également:
- Vérifier un élément dans une liste
- Liste déroulante excel - Guide
- Verifier compatibilite windows 11 - Guide
- Verifier un lien - Guide
- Liste déroulante en cascade - Guide
- Liste code ascii - Guide
2 réponses
Bonjour,
Je vois déjà 2 soucis. isdigit() est une fonction, il manque les () choice_customer.isdigit(). Après la réponse sur un input est une chaine, Choice est une liste d'entiers, 1 n'est pas égal à "1".
PS : il faut nous donner le code, pas une image, et pour afficher du code, il faut utiliser l'icone spécialisée <>, et donner le langage, Python, pour la coloration syntaxique.
Bonjour,
Comme jeepee l'indique, merci de partager ton code comme expliqué ici.
Pour préciser la réponse de jeepee, ton programme devrait plus probablement ressembler à ceci :
import sys def prompt_choice( choices: iter, message: str = "", cast: callable = None ): if cast is None: cast = lambda x: x choice = cast(input(message)) while choice not in choices: print(f"{choice} is an invalid choice (not in {choices}", file=sys.stderr) choice = cast(input(message)) return choice articles = [1, 2, 3] print(f"articles = {articles}") article = prompt_choice( articles, f"Choisir un article parmi {articles}: ", int ) print(f"article = {article}")
Résultat :
articles = [1, 2, 3]
Choisir un article parmi [1, 2, 3]: 4
4 is an invalid choice (not in [1, 2, 3]
Choisir un article parmi [1, 2, 3]: 2
article = 2
Bonne chance
Bonjour mamiemando,
mon code pour insérer un nouvel article ressemble à ça, comme je suis encore débutant je n'utilise que les fonctions de base que je comprends.
choice_number = 0 choice_list = [] #Pink Hair Doll choice_number += 1 choice_list.append(choice_number) PHD_number = choice_number PHD_price_hour = 5000.00 PHD_description = """ Pink Hair Doll, PHD for short. It does not grant you the Doctor title but you can make it a doctor. You can play with its hair and undress it anywhere. """ #Black Hellish chains choice_number += 1 choice_list.append(choice_number) BHC_number = choice_number BHC_price_hour = 3000.00 BHC_description = """ Black Hellish Chains, BHC for short. They burn so much that it can't be used to chain things up. Plastic gloves recommended. """ #White Stout Cat choice_number += 1 choice_list.append(choice_number) WSC_number = choice_number WSC_price_hour = 3500.00 WSC_description = """ White Stout Cat, WSC for short. Its way to defend your home makes it the perfect ferocious guard.""" #Print the choices print(""" EEEEE OOO RRRR ZZZZZ EEEEE A E O O R R Z E A A E O O R R Z E A A EEE O O RRRR Z EEE AAAAA E O O R R Z E A A E O O R R Z E A A EEEEE OOO R R ZZZZZ EEEEE A A ______________________________________________ """) print(f""" Hello and Welcome to Eorzea ! Choose your item and enjoy. >> {PHD_number}. {PHD_description} PRICE PER 1 HOUR ---- {PHD_price_hour} >> {BHC_number}. {BHC_description} PRICE PER 1 HOUR ---- {BHC_price_hour} >> {WSC_number}. {WSC_description} PRICE PER 1 HOUR ---- {WSC_price_hour} """) #Get the choices print("______________________________________________\nWhich one will you choose ? Type the corresponding number : ") while True: try: choice_customer = int(input()) if choice_list.count(choice_customer) > 0 : break elif choice_list.count(choice_customer) <= 0 : print("Please enter a valid number : ") except ValueError: print("Please enter a valid number : ") print("For how many hours ? : ") while True: try: hour_customer = int(input()) except ValueError: print("Please enter a valid number of hour (integer) : ")
Est-ce que c'est possible de le raccourcir pour mon niveau ?
Merci d'avance
Tu peux simplifier le programme précédent ainsi, il sera moins général mais fera ce que tu veux :
def prompt_choice(choices): choice = int(input(f"Choisir une valeur parmi {choices}: ")) while choice not in choices: print(f"{choice} is an invalid choice (not in {choices}") choice = int(input(f"Choisir une valeur parmi {choices}: ")) return choice articles = [1, 2, 3] print(f"articles = {articles}") article = prompt_choice(articles) print(f"article = {article}")
Bonne chance
Bonjour,
merci jee pee pour les conseils et les corrections. C'est mon premier post et déjà j'avais du mal à mettre une image .. bon,,,
Au final j'ai réussi à faire fonctionner mon code, j'ai demandé à un ami de m'aider. J'ai tendance à trop utiliser une boucle while.
Merci pour la réponse rapide !
Cette façon de procéder, avec une boucle, et la gestion des erreurs est nettement préférable à ta première proposition ;-)