0

hello im new to programing with python, i have this code arranged so a while loop can validate the Gamble variable in the start of the program and in the revaluation of the aformentioned variable. how can i make it so the the program works as is but without having it to write it twice?

def gamBling(Gamble):

        while Gamble == 'yes' or Gamble == 'Yes' or Gamble == 'y' or Gamble =='Y': 
             betTing()

             Gamble = input('Do you wish to play again? ')
             while  Gamble != 'yes' and Gamble != 'Yes' and Gamble != 'y' and Gamble != 'Y' and Gamble != 'No' and Gamble != 'no' and Gamble != 'N' and Gamble != 'n': 
                  Gamble = input('Please anwser in either yes or no? ')

        if Gamble == 'No' or Gamble == 'N' or Gamble == 'n' or Gamble == 'no': 
             print('okay, goodbye')
             exit()
print('Any bet you make will be doubled if your number is higher than or equal to 5. Want to play? ' + '(Yes or No)')

Gamble = input() 


while  Gamble != 'yes' and Gamble != 'Yes' and Gamble != 'y' and Gamble != 'Y' and Gamble != 'No' and Gamble != 'no' and Gamble != 'N' and Gamble != 'n': 

        Gamble = input('Please anwser in either yes or no? ')

gamBling(Gamble)

I want to be able to run the program the way it runs now but without having to repeat the while loop, if it can be done.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
bori_34
  • 3
  • 2
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ruohola May 11 '19 at 17:29

1 Answers1

0

You can define a separate function for validation and use it wherever you want

def validate(Gamble):
    while  Gamble != 'yes' and Gamble != 'Yes' and Gamble != 'y' and Gamble != 'Y' and Gamble != 'No' and Gamble != 'no' and Gamble != 'N' and Gamble != 'n': 
        Gamble = input('Please anwser in either yes or no? ')
    return Gamble      

Then your code will be like:

def gamBling(Gamble):

    while Gamble == 'yes' or Gamble == 'Yes' or Gamble == 'y' or Gamble =='Y': 
         betTing()
         Gamble = input('Do you wish to play again? ')
         Gamble = validate(Gamble)

    if Gamble == 'No' or Gamble == 'N' or Gamble == 'n' or Gamble == 'no': 
         print('okay, goodbye')
         exit()

print('Any bet you make will be doubled if your number is higher than or equal to 5. Want to play? ' + '(Yes or No)')
Gamble = input() 
Gamble = validate(Gamble)
gamBling(Gamble)
David Sidarous
  • 1,202
  • 1
  • 10
  • 25
  • @bori_34 Glad I could help! I recommend you accept the answer to help other not waste time trying to fix a fixed problem. Regards! – David Sidarous May 11 '19 at 18:29