简体   繁体   中英

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

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())

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.

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 if , not def
  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM