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
#2
So, you want someone to code the rest of your game, basically just the part with money, because when I tried playing it, it seems fine. We're not supposed to do your homework for you, so all I can do is help you. First, make a load function that tries to load money from the file, and if not gives them a set amount like below.
def loadData():
    with open("playMoney.txt", "r") as saveFile:
        money = saveFile.read()
        if not money or money == 0:
            print("Renewing money")
            return #Default amount
        return money
All you need to do is make a money variable in the main function, use function arguments and returns to pass it back and forth between functions, whenever the player wins, give them money, else, take away some money, or on a tie, neither. Then when they quit or their money runs out, execute a save function that should look something like this.
def save(money):
    with open("playMoney.txt", "w") as saveFile:
        saveFile.write(money)
It shouldn't take you too long to add this on to the game. I don't believe I made a mistake with the save and load functions, but if there's an error that you can't figure out just post about it on this question.
Reply
#3
(Apr-03-2020, 10:12 PM)SheeppOSU Wrote: So, you want someone to code the rest of your game, basically just the part with money, because when I tried playing it, it seems fine. We're not supposed to do your homework for you, so all I can do is help you. First, make a load function that tries to load money from the file, and if not gives them a set amount like below.
def loadData():
    with open("playMoney.txt", "r") as saveFile:
        money = saveFile.read()
        if not money or money == 0:
            print("Renewing money")
            return #Default amount
        return money
All you need to do is make a money variable in the main function, use function arguments and returns to pass it back and forth between functions, whenever the player wins, give them money, else, take away some money, or on a tie, neither. Then when they quit or their money runs out, execute a save function that should look something like this.
def save(money):
    with open("playMoney.txt", "w") as saveFile:
        saveFile.write(money)
It shouldn't take you too long to add this on to the game. I don't believe I made a mistake with the save and load functions, but if there's an error that you can't figure out just post about it on this question.

Thank you so much with the help of the rest of the game, you gave me the ideas of how to do the rest. But did you run my program various time? And did not encounter my problem that was second time of the play again when you input n, it keeps going on instead of stop right there
Reply
#4
I did not encounter problems but from the looks of your code I can say a few things. First, inside the while loop you need to redefine the again variable, otherwise it will use the same response. Also, you can use the lower function to compare. Instead of having a list of lower and uppercase letters. Just do this again.lower() == "y" there's also an upper function. Also, I'd like to point out that you can instead of doing gamePlayed = gamePlayed + 1 you can use gamePlayed += 1 which adds one to the original value, this also works with subtraction, division, etc.
Reply


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,076 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  How to compile following python loop program reinispl 3 1,910 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,639 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