-2

I am having a bit of a problem with my rock, paper, scissors game. I'm trying to make the for loop work but when there's a tie, it won't print what I wrote.

import random
#main function
def main():
    for game_count in range(5):
        answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))
        number = random.randrange(1,4)
        if answer == 1:
            position = "rock"
            print("You choose", position)
        elif answer == 2:
            position = "paper"
            print("You choose", position)
        elif answer == 3:
            position = "scissors"
            print("You choose", position)
        else:
            print("pick either 1, 2, or 3")
            return main()
#computer picks what to play    
        if number == 1:
            print("Computer chooses Rock")
        elif number == 2:
            print("Computer chooses Paper")
        elif number == 3:
            print("Computer chooses Paper")
        return number
#if there is a tie
        if answer == number:
            print("It's a tie. Go again")
main()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3852289
  • 21
  • 1
  • 4
  • 2
    After you `return number`, the function is finished - the last two lines never run. – jonrsharpe Oct 26 '14 at 09:36
  • 2
    It's a bad idea, to recursively call `main` if you only want to repeat the input. – Daniel Oct 26 '14 at 09:45
  • On that note, you may find http://stackoverflow.com/q/23294658/3001761 useful – jonrsharpe Oct 26 '14 at 09:49
  • This code highlights one of the *nastiest* things about Python. Van Rossum still had half of his life to live when he designed Python, and thought that `elif` would change the lives of all the people who had been typing `elsif`, `elseif`, `else if`, and even `else { if` all of their lives. The problem was that his was the wind that blew no one any good. I hope he's regretting it now as much as the rest of the world is. – Borodin Oct 26 '14 at 19:25

2 Answers2

1

It's unnecessary to write a main function, and it's certainly a bad idea to call it recursively to form a loop.

This program may interest you.

from random import randrange
from string import capitalize

weapons = [ 'rock',   'paper', 'scissors' ]
actions = [ 'blunts', 'wraps', 'cuts' ]

games = 0

while True:

    answer, position = None, None

    while True:
        answer = int(input("\nEnter 1 for Rock, 2 for Paper, 3 for Scissors: "))
        answer -= 1
        position = weapons[answer]
        if position:
            break
        print("Pick either 1, 2, or 3")

    print("You choose %s" % position)

    number = randrange(3)
    cpos = weapons[number]
    print("Computer chooses %s" % cpos)

    outcome = (answer - number) % 3

    if outcome == 0:
        print("It's a tie. Go again")
    elif outcome == 1:
        print(capitalize("%s %s %s" % (position, actions[answer], cpos)))
        print("Congratulations, you win!")
    elif outcome == 2:
        print(capitalize("%s %s %s" % (cpos, actions[number], position)))
        print("Computer wins!")

    games += 1
    if games == 5:
        break

print("Thanks for playing")
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • I'm puzzled here, I do like this implementation (would `+1`) but on the other hand it seems to me that user is "*implementing his homework*" and it doesn't provide any explanation why are pieces of his code ... "*useless*" and why stuff should be done differently (would `-1`). – Vyktor Oct 26 '14 at 10:43
  • 1
    @Vyktor: I agree with what you're saying. Since the problem has already been answered in the comments, my intention was to make a couple of relevant points and provide something the OP may find useful as a source of ideas. It is primarily a demonstration of my point that Python programs don't need a `main` entry point like Java and C code does, while being an indicator of what is possible in relatively few lines of code. I quite deliberately wrote something that couldn't be turned in as the OP's own work, and hoped that they may ask questions about it in the comments. – Borodin Oct 26 '14 at 10:50
0

I do like Borodins answer but I don't think it left anything out of your original code (be sure to check it out and study it carefully).

Let's make 4 points here:

  • you don't need to create main() function in python
  • don't call function recursively this way, use loop
  • you put return number on invalid place, without it it should work correctly
  • you don't need to check id by hand, you can user dictionary for this

So dictionary:

# You can easily replace this code:
if answer == 1:
    position = "rock"
    print("You choose", position)
elif answer == 2:
    position = "paper"
    print("You choose", position)
elif answer == 3:
    position = "scissors"
    print("You choose", position)

# By this
import random
positions = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

# Later in the code
if answer in positions:
    position = positions[answer]
    print('You choose', position)
else:
    # Invalid value

Avoiding recursion and using loop:

position = None
while position is None:
    answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))

    if answer in positions:
        position = positions[answer]
        print('You choose', position)
    else:
        print('pick either 1, 2, or 3')

And now just apply it to your code:

import random
positions = {
    1: 'rock',
    2: 'paper',
    3: 'scissors'
}

for game_count in range(5):

    # Pick a value for player
    position = None
    while position is None:
        answer = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))

        if answer in positions:
            position = positions[answer]
            print('You choose', position)
        else:
            print('pick either 1, 2, or 3')


    # Computer picks what to play    
    number = random.randrange(1,4)
    print('Computer chooses', positions[number]) # Again, dictionary

    # NO return number here

    # if there is a tie
    if answer == number:
        print("It's a tie. Go again")
        continue

    # do more comparisons to decide who wins

I also assume you want to have 5 winning games (retry on tie), so just rewrite loop condition:

# Instead of
# for game_count in range(5):
games_to_go = 5
while games_to_go > 0:
    # ... 
    # if there is a tie
    if answer == number:
        print("It's a tie. Go again")
        continue

     # decrease number of games to go
     games_to_go -= 1
Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96