Python Forum
RockPaperScissor program while loop malfunction
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RockPaperScissor program while loop malfunction
#1
import random
def introduction():
    intro = "Welcome to the game of Rock, Paper, Scissors" + "\n" "This is a final project created by the Author: Tony Liang " + "\n" "Have a good game!!!"

    return intro
    
def generateRandomNumber():
    randomNumber = random.randint(1,3)
    return randomNumber

def computer(randomNumber):
    
    if randomNumber == 1:
        computer = "R"

    elif randomNumber == 2:
        computer = "P"

    elif randomNumber == 3:
        computer = "S"

    return computer


def player():
    player = input("Enter your choice from Rock, Paper or Scissors: ")
    while player not in (["Rock","Paper","Scissors"]):
            print("Please try again and enter a valid choice")
            player = input("Enter your choice from Rock, Paper or Scissors: ")

    if player == "Rock":
        player = "R"

    elif player == "Paper":
        player = "P"

    elif player == "Scissors":
        player = "S"

    return player

def rule(computer,player):
    rockMes = "The rock smashes the scissors"
    scissorMes = "Scissors cuts paper"
    paperMes = "Paper wraps rock"
    winner = ""
    message = ""

    win = 0
    lose = 0
    draw = 0

    if player == computer:
        winner = "no winner"
        message = ""
        draw = draw + 1        
        

    elif player == "R":
    
        if computer == "S":
            winner = "Player"
            message = rockMes
            win = win + 1
        elif computer == "P":
            winner = "Computer"
            message = paperMes
            lose = lose + 1

    elif player == "P":
        
        if computer == "R":
            winner = "Player"
            message = paperMes
            win = win + 1
        elif computer == "S":
            winner = "Computer"
            message = scissorMes
            lose = lose + 1

    elif player == "S":
        
        if computer == "P":
            winner = "Player"
            message = scissorMes
            win = win + 1
        elif computer == "R":
            winner = "Computer"
            message = rockMes
            lose = lose + 1

    return winner, message, win, lose, draw

def gameAnalysis(win,lose,draw):
    return str(win)+"W", str(lose)+"L", str(draw)+"D"

def startOver():
    randomNumber = generateRandomNumber()
    computerChoice = computer(randomNumber)
    playerChoice = player()
    winner,message,win,lose,draw = rule(computerChoice,playerChoice)
    analysis = gameAnalysis(win,lose,draw)
    gamePlayed = 0
    
    print("The computer chose", computerChoice)
    print("You chose", playerChoice)

    if winner != "no winner":
        gamePlayed = gamePlayed + 1
        print(winner,"won(",message,")""\n")
        again = "Y","y","n","N"
        again = input("Do you want to play again (Y/N):")
         
        while again == "Y" or again == "y":
            print(startOver())
            gamePlayed = gamePlayed + 1
        if again == "n" or again =="N":
            winner = " "
            return analysis, str(gamePlayed)+"# Played"

    while winner == "no winner" :
        print("You both chose the same thing, try again")                
        print(startOver())

def main():
    print(introduction())
    randomNumber = generateRandomNumber()
    computerChoice = computer(randomNumber)
    playerChoice = player()
    winner,message,win,lose,draw = rule(computerChoice,playerChoice)
    analysis = gameAnalysis(win,lose,draw)
    gamePlayed = 0
    
    print("The computer chose", computerChoice)
    print("You chose", playerChoice)
    
    if winner != "no winner":
        gamePlayed = gamePlayed + 1
        print(winner,"won(",message,")""\n")
        again = "Y","y","n","N"
        again = input("Do you want to play again (Y/N):")
        
        while again == "Y" or again == "y":
            print(startOver())
            gamePlayed = gamePlayed + 1
        if again == "n" or again =="N":
            winner = " "
            return analysis, str(gamePlayed)+"# Played"
            
    while winner == "no winner" :
        print("You both chose the same thing, try again")                
        print(startOver())    

print(main())
This is my code

This is my traceback
Output:
Welcome to the game of Rock, Paper, Scissors This is a final project created by the Author: Tony Liang Have a good game!!! Enter your choice from Rock, Paper or Scissors: Rock The computer chose P You chose R Computer won( Paper wraps rock ) Do you want to play again (Y/N):n (('0W', '1L', '0D'), '1# Played')
Output:
Welcome to the game of Rock, Paper, Scissors This is a final project created by the Author: Tony Liang Have a good game!!! Enter your choice from Rock, Paper or Scissors: Rock The computer chose R You chose R You both chose the same thing, try again Enter your choice from Rock, Paper or Scissors: Rock The computer chose P You chose R Computer won( Paper wraps rock ) Do you want to play again (Y/N):n (('0W', '1L', '0D'), '1# Played') You both chose the same thing, try again Enter your choice from Rock, Paper or Scissors:
The requirements are following:

1.When begins, computer choose r, p , s according to 1 to 3 by using randint
2.Then player enters their choice of r, p, s with input validation
3.Winner is selected according to:
R>S
S>P
P>R
If same then must be played again
4.Computer`s choice and winner are displayed
5.Before it starts, program will display an introduction
6.Player can choose to play more than one game (while)
7.When Player stops playing, it will display game session (#wins,#loses, #games played for player only)
I had problem with 7. since the wins,loses, players only counts ones (does not repeat after 2nd iteration)
8.Player has a limited amount of money to play with (this amount is stored in a file playMoney.txt)
9.Before one game,player can bet any amount of money not higher than his play money
10. If player wins one game,win the bet money; otherwise they lose the bet moeny
11. After players stops playing, or when they have lost all their money, game session will be displayed
amount of play money left will be written(not appended ) to the file playMoney.txt

Since this is kind of beginner hw, please just use while, for,list,filewriting, or basic functions
And do not use break and continue,since my teacher hate it


Please anyone who can help with this homework, already spent 6h on this and I'm so dizzy now. It is due 11:55PM, rn is 2:45PM, and still have work later.... plz help with this and thank you so much!
Reply


Messages In This Thread
RockPaperScissor program while loop malfunction - by tonyliang19 - Apr-03-2020, 10:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,215 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  How to compile following python loop program reinispl 3 1,980 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Program that, inside a loop, does multiple things. needs to print in a certain way reidmcleod 1 2,697 Feb-19-2019, 02:35 PM
Last Post: marienbad

Forum Jump:

User Panel Messages

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