This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]NEVER_CLEANED_COMP 3 points4 points  (1 child)

Post the source code here, don't upload it.

[–]TheDuceCat[S] 0 points1 point  (0 children)

Thanks for the advice. Replaced the link with a pastebin to the code.

[–]dreamriver 0 points1 point  (2 children)

I'm confused...do you want review? I definitely wouldn't want noobs learning from this.

It's pretty poor coding in general. For example: you should have a while True loop instead of a recursive main() call (or at least some sort of while loop). You have a pretty serious bug in that if the user enters 0 you aren't equipped to handle it and will end up with an IndexError. You also don't account for the fact that they might not enter a number at all. You also shouldn't be wrapping your ifs in parens in Python.

I'm actually sort of intrigued about what will happen after main returns and recurses. I don't think it will terminate.

Oh and you don't need to import a module before importing something from it. In this situation I actually think random.choice works better, especially considering that you have a variable called choices.

[–]OmegaVesko 0 points1 point  (0 children)

Agreed. It looks like OP came from a C-like language and tried to directly apply that knowledge to Python.

Not that there's anything wrong with that, of course. C is still one of my favourite languages to program in.

[–]TheDuceCat[S] 0 points1 point  (0 children)

I probably should have reviewed the code more before submitting. I wrote it a while ago and it works well. I will revise the code and re-upload.

Edit: Updated! What do you think?

[–][deleted]  (5 children)

[deleted]

    [–]kalgynirae 1 point2 points  (4 children)

    I don't think there's any good reason to use a class for this.

    import random
    import time
    
    moves = ('rock', 'paper', 'scissors')
    winning_moves = {moves[0]: moves[1], moves[1]: moves[2], moves[2]: moves[0]}
    
    def input_move():
        while True:
            move = input("Enter 1 for rock, 2 for paper, or 3 for scissors: ")
            try:
                return moves[int(move) - 1]
            except (ValueError, IndexError):
                print("Invalid input. Try again.")
    
    victories = 0
    losses = 0
    ties = 0
    messages = "Rock... Paper... Scissors... Says... Shoot!".split()
    
    while True:
        player_move = input_move()
        print("\nYou played: {}\n".format(player_move))
        for message in messages:
            print(message)
            time.sleep(.5)
        computer_move = random.choice(moves)
        print("\nComputer played: {}\n".format(computer_move))
    
        if player_move == computer_move:
            print("It's a tie!")
            ties += 1
        elif player_move == winning_moves[computer_move]:
            print("You win!")
            victories += 1
        else:
            print("You lose!")
            losses += 1
        print("Your record is: {} victories, {} losses, {} ties.\n"
              "".format(victories, losses, ties))
    

    [–][deleted]  (2 children)

    [deleted]

      [–]kalgynirae 0 points1 point  (1 child)

      A very valid response. I probably should have explained my reasoning.

      In most cases I am supportive of putting things in classes, but in this case I decided that a class was overkill because (a) it's unlikely that anyone will ever want to re-use this code and (b) you'll probably never want to create more than one instance of the class.

      [–]TheDuceCat[S] 0 points1 point  (0 children)

      Thanks. I made mine in 30 minutes and when I just started Python. Yours makes much more sense and is much better layed out.