Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop inconsistency
#1
import random

print("Welcome to ROCK PAPER SCISSORS!")
selecter = random.randint(1, 3)

computer_choice = ""


computer_score = 0
player_score = 0
computer_choice_history = []
playing = True
while_counter = 0


while playing:
    while_counter += 1
    if computer_score or player_score == 3:
        playing = False

    if while_counter > 2:
        if computer_choice_history[0] == computer_choice_history[1]:
            selecter = random.randint(1, 3)

    # computer and player choices
    player_choice = input("\nchoose rock, paper or scissors:\n")
    if selecter == 1:
        computer_choice = "rock"
    elif selecter == 2:
        computer_choice = "paper"
    elif selecter == 3:
        computer_choice = "scissors"
    print(computer_choice)
    computer_choice_history.append(selecter)

    # computer wins round
    if computer_choice == "rock" and player_choice == "scissors":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))
    elif computer_choice == "scissors" and player_choice == "paper":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))
    elif computer_choice == "paper" and player_choice == "rock":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))

    # If both chose the same
    if computer_choice == player_choice:
        print("Tie")

    # player wins round
    elif player_choice == "rock" and computer_choice == "scissors":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))
    elif player_choice == "scissors" and computer_choice == "paper":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))
    elif player_choice == "paper" and computer_choice == "rock":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))

if computer_score == 3:
    print("\nComputer wins, YOU LOSE")
elif player_score == 3:
    print("\nCongrats You Win!")

satisfaction = input("\nare you satisfied?\n")
Hi,
The above code is meant to repeat until either the 'computer_score' or the 'player_score' are equal to 3, but for some reason the while loop ends randomly and I'm not sure what is causing it.

Any suggestions?
Reply
#2
while loop does exactly what has been told to do:

if computer_score or player_score == 3:
    playing = False    
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
In your while loop you put if computer_score or player_score == 3 which is not how Python works. Python interprets these as two separate statements: "if computer_score" and "if player_score == 3". If computer_score does not equal 0 then the if statement passes. So it should be if computer_score == 3 or player_score == 3. Other than that I'd like to add a few things about your code. First off would be the game itself. There's no system to check a player's answer. If they type some random thing, then the game is gonna mess up. A simple system to check that would look like this:
answerList = ['yes', 'no']
gettingAnswer = True
while gettingAnswer:
    answer = input("Say yes or no:\n")
    if answer in answerList:
        gettingAnswer = False
The second thing would be your code. In programming not only do you have to write code that works but you should also write code that's efficient, easy to work with, etc. Otherwise making changes can be tough and as the code for it grows it gets harder to manage. So I wrote a quick example below with comments on ways to write the code more efficiently
import random

choiceList = ['s', 'r', 'p'] #List of choices
scores = {
    "plr": 0,
    "com": 0,
}

def getPlayerInput():
    while True:
        answer = input("Pick r, p, or s:\n").lower() #Lower case it in case they decide to type uppercase
        if answer in choiceList: #If their answer is valid
            return answer #Return the player's answer
        else:
            print("Invalid answer") #Tell them their answer was invalid

def main():
    choiceDict = {"rp": "com", "rs": "plr",
                  "sp": "plr", "sr": "com",
                  "ps": "com", "pr": "plr"} #List of choices that can be used to see who won
    feedbackDict = {
        "rp": "Paper wraps rock, the computer won this round", "rs": "Rock smashes scissors, you won this round",
        "sp": "Scissors cuts paper, you won this round", "sr": "Rock smashes scissors, the computer won this round",
        "ps": "Scissors cuts paper, the computer won this round", "pr": "Paper wraps rock, you won this round" #Tells the program what to say for each situation
    }
    while True:
        plrChoice = getPlayerInput() #Get the input from the player
        comChoice = random.choice(choiceList) #Get the computer's choice
        if plrChoice == comChoice: #If they're the same
            print("You tied")
            continue #Continue back to the top of the while loop
        scores[choiceDict[plrChoice+comChoice]] += 1 #Add to the either the plr's or computer's score
        print(feedbackDict[plrChoice+comChoice]) #Print the feedback
        if scores["plr"] == 3:
            print("You win the game!")
            break
        elif scores["com"] == 3:
            print("You lost the game...")
            break

if __name__ == "__main__": #If this file is not being imported (It's the main program)
    main() #Run main function
Hope this helps
Reply
#4
wow thank you for the suggestions and the correction.
Reply


Forum Jump:

User Panel Messages

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