1

I am learning Python and am trying to take concepts that I learn from video tutorials and add to them. I just watched a video on If Statements and Comparisons, and I wanted to add to what was done in the video by getting input from the user. I received the "shadows name 'ans' from outer scope" warning and have seen others ask this question on the site, but their examples do not involve getting input from the user. Thank you in advance for your help!

ans = input("What color is the sky? ")


def color(ans):
    if ans == str("Blue"):
        return str("Correct!")
    else:
        return str("Incorrect.")


print(color(ans))

1 Answers1

3

First things first - the warning is specific to pycharm and your code should run correctly as it is.

Now, there are two ways how you can get rid of the warning:

  • Either you can rename the argument used within the function i.e. instead of giving it same name ans, you can opt for a different name e.g. answer.

  • The other way could be suppress this warning in pycharm:

    # noinspection PyShadowingNames
    def color(ans):
        # rest of the code...
    
AKS
  • 18,983
  • 3
  • 43
  • 54
  • I know that this does not impede the ability of the code to successfully run. I just want to make sure my code is clean. I would not want to start any bad habits that would mess me up in the long run. Also, thank you for the suggestion. I thought that if I renamed ans to answer inside of the function that it would hit me with the "unresolved reference" error. However, I see that that is not the case. So thank you! – target_unknown Jul 28 '21 at 03:15