Python Forum
Problem restricting user input in my rock paper scissors game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem restricting user input in my rock paper scissors game
#1
Okay, I am a new Python Programmer, I am a beginner, I started doing this a few months ago. I have been working through various Python challenges I found on the internet to practice and learn. So, I am working on this one, it is a rock, paper scissors game, and I managed to get the code to work. But in order for me to call it complete I felt I needed to add some code to ensure that the user only enters in one of the three, rock, paper or scissors.

If figured at first that a while loop would work great for this, so I tried it, and here is the code:
def start():
player1 = input("Player one, What is your name? ")
user1="rock"
while user1 != "rock" or user1 != "paper" or user1 != "scissors":
user1 = input(player1 + ", you are up, what's it going to be, rock, paper or scissors? ")
user1 = user1.lower()
clear()
player2 = input("Player two, What is your name? ")
user2 = input(player2 + ", you are up, what's it going to be, rock, paper or scissors? ")
user2 = user2.lower()
return (player1, user1, player2, user2)

Please note, that I only added the while loop for the first input because I wanted to test it makes sure it worked before adding it for player two. This somehow makes the loop infinite and no matter what I enter it cycles back to the same question. I ran it through a debugger and it appears that as though my boolean arguments are being ignored. I figured that as long as the input was NOT rock, paper, or scissors then it should stay in the loop. if it's rock or paper or scissors it should exit the loop. Apparently this is not the case. Any suggestions?

I would appreciate more knowledge on how this loop works, most of the information i found in the internet was very simple with regards to this while loop so it may only work with single arguments, but this is why I am reaching out to you guys for help.

Okay, so failing that I figured an IF statement might work instead so I changed the code to read like this:
def start():
player1 = input("Player one, What is your name? ")
def player1_input():
user1 = input(player1 + ", you are up, what's it going to be, rock, paper or scissors? ")
user1 = user1.lower()
if user1 != "rock" or user1 != "paper" or user1 != "scissors":
print("Please enter one of the three: Rock, Paper or Scissors")
player1_input()
return user1
player1_input()
clear()
player2 = input("Player two, What is your name? ")
user2 = input(player2 + ", you are up, what's it going to be, rock, paper or scissors? ")
user2 = user2.lower()
return (player1, user1, player2, user2)

UGGH!!! Same problem!! So now I know that there is a problem with my conditions in that line. So it's not the while loop, it is the way I have written it. Please explain why this isn't working.

Okay, so here's the thing. I could have simply done it as a multiple choice, by having them enter 1, 2 or 3, and assigning the rock, paper scissors to each number and got it to work that way but I chose not to do that as I wanted to learn more about how to do exactly what I am explaining right now. Using the 1,2,3 idea was too easy basically.

So the goal is to allow the program to continue execution when one of the three words are entered and reject the rest. I am open to other methods and suggestions of how to do this other then the multiple choice input I had mentioned above.

I have written the code using the https://www.onlinegdb.com/, and when I am at home (I practice at work too, that's why I use GDB), I was using the Thonny IDE, so, the latest version of Thonny is python 3.7 and gdb, I never checked.

Thank you for you help.
Reply


Messages In This Thread
Problem restricting user input in my rock paper scissors game - by ashergreen - Mar-18-2021, 11:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,721 Feb-23-2024, 03:14 PM
Last Post: DPaul
  problem in using input command akbarza 4 999 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,030 Dec-11-2022, 07:39 PM
Last Post: snippsat
  Trying to create a visual rock paper scissors game urmom33 1 976 Dec-03-2022, 09:12 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,837 Sep-11-2022, 12:36 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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