Skip to content
Advertisement

Fixing a while-loop to only run once

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

Advertisement

Answer

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

Advertisement