Reputation: 3
I am trying to create a simple Rock, Paper, Scissors game to practice some basic Python skills. I created the code below to take user input and create a simple point-based game. However, when I ran the code, the loop ran infinitely, printing the output message until I force stopped it. How can I alter my code so the while-loop only runs once per user input?
code:
import random as rand
print("Welcome to Rock, Paper, Scissors!")
x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")
game_choices = ("Rock",
"Paper",
"Scissors")
comp_input = rand.choice(game_choices)
phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"
my_score = 0
print("The computer chose: ",comp_input)
while my_score >= 0:
if x == "R" and comp_input == "Rock":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Scissors":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Paper":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Scissors":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Paper":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Rock":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Paper":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Rock":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Scissors":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
Upvotes: 0
Views: 71
Reputation: 6090
You need to set your while loop before the user input, otherwise it is just looping over and over the same inputs values:
import random as rand
print("Welcome to Rock, Paper, Scissors!")
my_score = 0
while my_score >= 0:
x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")
game_choices = ("Rock",
"Paper",
"Scissors")
comp_input = rand.choice(game_choices)
phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"
print("The computer chose: ",comp_input)
if x == "R" and comp_input == "Rock":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Scissors":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Paper":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Scissors":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Paper":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Rock":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Paper":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Rock":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Scissors":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
Below you get a little more compact version by the way:
import random as rand
print("Welcome to Rock, Paper, Scissors!")
phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"
my_score = 0
while my_score >= 0:
x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")
game_choices = ("Rock",
"Paper",
"Scissors")
comp_input = rand.choice(game_choices)
print("The computer chose: ",comp_input)
#You store the choices in the same list
match = [x[0],comp_input[0]]
#set operation provides you with unique elements
#If the user and comp inputs char are the same, then length = 1
if len(set(match)) == 1:
print(phrase_one)
print("Score: ",my_score)
#If match in combinations that makes the user win
elif match in [['R','S'],['S','P'],['P','R']]:
print(phrase_two)
my_score += 1
print("Score: ",my_score)
#Other situations are equivalent to loose
else:
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
#You don't need break statements in your code, btw (while condition)
Here, if user choice is "Rock" and computer choice "Paper", you store the value for the first index of each string in a list called match.
choice_1 = "Rock"
choice_2 = "Paper"
match = [choice_1[0],choice_2[0]] # match = ["R","P"]
match = set(match) # Yield match = ["R","P"], to test against winning/loosing combinations
choice_1 = "Rock"
choice_2 = "Rock"
match = [choice_1[0],choice_2[0]] # match = ["R","R"]
match = set(match) # Yield match = ["R"], it is a tie because len(match) = 1
Upvotes: 2
Reputation: 288
the problem is that unless you loose on your first round you keep winning for ever because none of the input values changed. all you have to do is move your input() and the computers random selection into the loop like this
import random as rand
print("Welcome to Rock, Paper, Scissors!")
game_choices = ("Rock",
"Paper",
"Scissors")
phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"
my_score = 0
while my_score >= 0:
x = input("\nYour move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ").upper()
comp_input = rand.choice(game_choices)
print("The computer chose: ",comp_input)
if x == "R" and comp_input == "Rock":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Scissors":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Paper":
print(phrase_one)
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Scissors":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "R" and comp_input == "Paper":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Rock":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "S" and comp_input == "Paper":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Rock":
print(phrase_two)
my_score += 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
elif x == "P" and comp_input == "Scissors":
print(phrase_three)
my_score -= 1
print("Score: ",my_score)
if my_score < 0:
print("Game over :( Also, YOU SUCK!")
break
Upvotes: 0