Jan-19-2021, 12:04 PM
(Jan-19-2021, 08:52 AM)buran Wrote: validate the user inputThank you! they both helped a lot.
I did this:
player1 = input("Give player 1 a name:\n") player2 = input("Give player 2 a name:\n") choices = ['rock', 'paper', 'scissors'] def move(player): while True: choice = input(f"{player}, choose one:(rock/paper/scissors) ") if choice in choices: return choice print("Check the spelling.") wins = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper' } while True: choice1 = move(player1) choice2 = move(player2) print(f"{player2} wins!" if choice1 == wins[choice2] else f"{player1} wins!" if choice2 == wins[choice1] else "Same!") if input("Continue?(y/n) ") == 'n': breakOut of curiosity, now in the
def move(player):I validate the input which is in the function itself.
But can I define a validation function that uses 'while True' to validate an input that is defined outside of it? Like calling a validate function on any output that is outside of the function?!
Something like this:
answer = input("some stuff here: ") def validate(parameter): while True: if parameter is blah blah: break print("correct the answer.") #some code to return to the answer outside #Now calling the validate function on answer validate(answer)