I want to Remove a print function if Someone choose another option in python
Question:
I have three print function in python basically these a option 1 2 3 if someone choose 1 after that when it again come to these steps beacause it is infinite loop i want to remove option 3 from there basically i want to remove print function.
Please help me .
Thanks in Advance .
Is it ok if i use a if else here !!!!!!
this is portion where i want to do that
In this i want third print removed after if Someone chose choice 1
self.blackjack = False
print(f" Press 1 For Hit a New Card ")
print(" Press 2 For Stand ")
print(" Press 3 for Double ")
choice = int(input(" Enter Your Choice: "))
if choice == 1:
running = self.hitTheCard()
elif choice == 2:
self.stand()
running = False
elif choice == 3:
self.betAmount *= 2
running = self.hitTheCard()
self.player.displayCardInHand()
if running:
self.stand()
running = False
else:
print(" Invalid choice given try again.")
Feel free give me advices please
Answers:
Put your options in a dictionary; then you can loop over the dictionary to display the available options, and delete them from the dictionary when you want them to no longer be available.
options = {
'1': ("Hit a New Card", self.hitTheCard),
'2': ("Stand", self.stand),
'3': ("Double", self.double),
}
while True:
for opt, (desc, _func) in options.items():
print(f"Press {opt} for {desc}")
choice = input("Enter your choice: ")
if choice not in options:
print("Invalid choice, try again.")
continue
_desc, func = options[choice]
del options[choice]
func()
I have three print function in python basically these a option 1 2 3 if someone choose 1 after that when it again come to these steps beacause it is infinite loop i want to remove option 3 from there basically i want to remove print function.
Please help me .
Thanks in Advance .
Is it ok if i use a if else here !!!!!!
this is portion where i want to do that
In this i want third print removed after if Someone chose choice 1
self.blackjack = False
print(f" Press 1 For Hit a New Card ")
print(" Press 2 For Stand ")
print(" Press 3 for Double ")
choice = int(input(" Enter Your Choice: "))
if choice == 1:
running = self.hitTheCard()
elif choice == 2:
self.stand()
running = False
elif choice == 3:
self.betAmount *= 2
running = self.hitTheCard()
self.player.displayCardInHand()
if running:
self.stand()
running = False
else:
print(" Invalid choice given try again.")
Feel free give me advices please
Put your options in a dictionary; then you can loop over the dictionary to display the available options, and delete them from the dictionary when you want them to no longer be available.
options = {
'1': ("Hit a New Card", self.hitTheCard),
'2': ("Stand", self.stand),
'3': ("Double", self.double),
}
while True:
for opt, (desc, _func) in options.items():
print(f"Press {opt} for {desc}")
choice = input("Enter your choice: ")
if choice not in options:
print("Invalid choice, try again.")
continue
_desc, func = options[choice]
del options[choice]
func()