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