Home > Software engineering >  part of my code is unreachable in vscode, Iam trying to build basic rock paper scissors game
part of my code is unreachable in vscode, Iam trying to build basic rock paper scissors game

Time:06-16

I am working on rock paper scissors game in order to learn python but my code is being greyed out/unnreachable, how would you solve it?

import random

def play():
    user = input("What is your choice'r' for rock, 'p' for paper, 's' for scissors\n")
    computer = random.choice(['r','p','s'])

    if user==computer:
        return 'tie'        

        def is_win(player, opponent):
            return 'You won'
    
    return 'You lost'

#FROM HERE IT IS UNREACHABLE

def is_win(player, opponent):
  
    if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \
        or (player == 'p' and opponent == 'r'):
        return True 

        print(play())

CodePudding user response:

Here:

def is_win(player, opponent):

    if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \
        or (player == 'p' and opponent == 'r'):
        return True 

        print(play())

You have the last line inside this if block placed AFTER the return statement: once a function has returned a value, it exits from execution.

Also, that indentation gives place to a call loop, since play() itself calls is_win().

You should put play() - not print(play()) - with no indentation in that .py file to make the program run.

In VS Code, there are tools for improving your indentation.

Edit: I now see that you have declared is_win() also within play(), probably with the intent of calling it. I suggest you to look online for refreshing your Python basic skills and examples for programs of this kind.

CodePudding user response:

I tried to regenerate your code

There were three wrong places at the same place

def is_win(player, opponent):
    return 'You won'
  1. Here it should be 1if, notdef`
  2. You have added extra indent to this block
  3. The arguments should be user,computer , not player,opponent

And the last one. I don't know whether it's a typing mistake happened while posting the question here.

  1. print(play()) should be outside of the is_win function
  • Related