Deb Orah A
Deb Orah A

Reputation: 13

Problems with Rock, Paper, Scissors Python Code

I'm working on a code with python, where we've been asked to create a game of Rock, Paper, Scissors using an external file of options (rock, paper, scissors) as opposed to having the code use any user input. However, for some reason, my code doesn't work. When someone inputs "yes", it prints "Let us play now," but that's it. Nothing else happens.

Why isn't the game completing when a user provides "yes" as input?

from random import randrange
def sample():
    computer_input = randrange(1,3)
    return computer_input

def main():
    a = [] 
    infile = open("input2.txt", "r")
    for line in infile:
        a.append(line)
    computer_input = sample()

    tied = 0 #games tied
    user_won = 0 #games won by user
    comp_won = 0 #games won by computer

    user_input = ""
    computer_input = ""

    print("Rules of the game...")
    print("Would you like to turn up with a game of rock, paper, or scissors? ;) Yes or no? -->")
    answer = input()
    if (answer == "yes"):
        play = True
        print("Let us now play.")

##    elif(answer == "no" or "No"):
##        play = False
##        print("Sorry. Maybe we can play next time ;)")
##    else:
##        play = False
##        print("Please try again!")
##        main()

    while True:
        if(computer_input == "1"):
            if(user_input == a[0]):
                tied = tied + 1 
                print("Game is tied!")        
            elif(user_input == a[1]):
                user_won = user_won + 1
                print("You won! Paper covers Rock")        
            elif(user_input == a[2]):
                comp_won = comp_won + 1
                print("You lost! Rocks knocks out scissors")       
##            else:
##                print("Try again!")    
        elif (computer_input == "2"):
            if (user_input == a[0]):
                comp_won = comp_won + 1
                print("You lost! Paper covers Rock")
            elif(user_input == a[1]):
                tied = tied + 1 
                print("Game is tied!")
            elif(user_input == a[2]):
                user_won = user_won + 1
                print("You won! Scissors cuts Paper")
##            else:
##                print("Try again!")       
        else :
            if(user_input == a[0]):
                user_won = user_won + 1
                print("You won! Rock knocks out scissors")
            elif(user_input == a[1]):
                comp_won = comp_won + 1
                print("You lost! Scissors cuts Paper")
            elif(user_input == a[2]):
                tied = tied + 1 
                print("Game is tied!")
##            else:
##                print("Try again!")
##                


##print("Game over")
##print("Statistics")
##print("Games tied -->", tied)
##print("Game won by comp -->", comp_won)
##print("Game won by user -->", user_won)
##        

main()

Upvotes: 1

Views: 211

Answers (2)

David K
David K

Reputation: 3132

There are many, many problems with this code.

Every time you define or use a variable, you should have a clear idea of why it is defined this way or exactly what this use of the variable is going to accomplish.

You have a loop, which seems to indicate that you meant for more than one round of the game to be played when you run the code once. But there is only one place where the computer's choice is set to a number 1, 2, or 3, and it occurs only once. (Besides, as has already been pointed out, then you change the computer's choice to "" without even reading the number once.)

You have no apparent way to get out of the loop within the logic of the code.

It is unclear what you think you are supposed to be reading from the user's input file. You put the contents of the file in the array a line by line, but then you only ever look at a[0], a[1], and a[2]. What were the first three lines of the file supposed to contain? Why only three lines? What does it mean to ask whether user_input == a[0]? (I have a hunch that you were supposed to set the user input on each round to a member of a, not compare user_input to a member of a.)

(Also notice that you set user_input = "" earlier, so unless you read empty strings into the entries of a, expressions like user_input == a[0] will always be false.)

What is the point of setting play = True? (Or even setting play = False as in the commented-out code?) You never do anything that would read the value of play.

What is the point of keeping count in the variables tied, user_won, and computer_won? You never read those variables either, except when you're setting new values of them.

It might help if you write some smaller functions with very clear purpose, input and output. The sample() function is promising, but everything else is in main(). For example, figuring out who won, given the computer's choice and the player's choice, could be a function. By writing such a function you would remove dozens of lines of code from the loop in main(), replacing them with perhaps one line of code. It's much, much easier to write well-designed loops when the block of code inside the loop is short.

Upvotes: 1

Hamms
Hamms

Reputation: 5107

Notice that on line 5 below, you set computer_input to the result of sample():

def main():
    a = [] 
    infile = open("input2.txt", "r")
    for line in infile:
        a.append(line)
    computer_input = sample()

But then a few lines later, you set it to "":

    user_input = ""
    computer_input = ""

Your while loop is checking against the value of computer_input, but it assumes it will be a number. It has no case to handle the empty string. I would recommend removing that line.

Also note that your while loop is checking the value of user_input, but you never seem to actually read input into that variable.

Upvotes: 1

Related Questions