Mar-18-2021, 11:28 AM
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.
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.