def confirm_choice():
confirm = input("[c]Confirm or [v]Void: ")
if confirm != 'c' and confirm != 'v':
print("\n Invalid Option. Please Enter a Valid Option.")
confirm_choice()
print (confirm)
return confirm
When an invalid input has been keyed in for example, the letter 'k' followed by a valid input 'c', the function would print both inputs 'c' and 'k'
Output:
c
k
How can the above program be altered so that it returns only either 'c' or 'v'and repeats the function if the input is invalid.
Recursion is unnecessary; it's easier to use a while loop for this:
while True:
confirm = input('[c]Confirm or [v]Void: ')
if confirm.strip().lower() in ('c', 'v'):
return confirm
print("\n Invalid Option. Please Enter a Valid Option.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With