Python Forum
Rock, paper, scissors spelling error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock, paper, scissors spelling error
#6
(Jan-19-2021, 12:04 PM)banidjamali Wrote: 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?!

You can always write a validation function that will take an argument and return True or False, based on some conditions. You don't need a loop though - you take one argument and check that argument against some conditions (i.e. no need of the loop at all)

Also, writing validation function makes sense if you perform some more complex validation. In this case it will not add much benefit and it can even be argued it complicates the code unnecessarily (i.e. compare if some_input in valid_inputs: vs if is_valid(some_input): where is_valid(param) is function that will do just return param in valid_inputs

couple of other things
  • in your function it would be better if choices is defined inside the function, or provided as argument.
  • something like this only makes the code more difficult to read and follow:
        print(f"{player2} wins!" if choice1 == wins[choice2] else f"{player1} wins!" if choice2 == wins[choice1] else "Same!")
  • many (me too) don't like one-line ifs like if choice in choices: return choice
player1 = input("Give player 1 a name:\n")
player2 = input("Give player 2 a name:\n")
 

def move(player):
    choices = ['rock', 'paper', 'scissors']
    while True:
        choice = input(f"{player}, choose one:({'/'.join(choices)}) ")
        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)
    if choice1 == choice2:
        print('Same')
    else:
        winner = player1 if choice2 == wins[choice1] else player2
        print(f'{winner} wins')

        # alternatively
        if choice2 == wins[choice1]:
            print(f'{player1} wins')
        else:
            print(f'{player2} wins')

    if input("Continue?(y/n) ") == 'n':
        break
banidjamali likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Rock, paper, scissors spelling error - by buran - Jan-19-2021, 08:52 AM
RE: Rock, paper, scissors spelling error - by buran - Jan-19-2021, 01:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 3,026 Jun-24-2021, 08:00 PM
Last Post: deanhystad
  Rock, Paper, Scissors Game kramon19 2 5,497 Jan-10-2020, 08:18 AM
Last Post: perfringo
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 6,037 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,325 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,286 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,571 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 3,029 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,279 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,198 May-19-2017, 10:56 PM
Last Post: ichabod801
  Rock Paper Scissors game codeobri 3 13,585 Apr-28-2017, 01:02 AM
Last Post: codeobri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020