'Name error when name is defined? For class project
Thank you for taking time to read this. I have a project due for my Programming class by Friday. The project is basically analyzing the most popular songs around the world. The user will input W is they want to see the most popular genre worldwide and how many streams it has. My program consists of two python files, one that contains the top 10 song list, and the other where the user will input their options. Here is my file for my top10songs:
def Mostpopularw(Pop):
Pop =='Pop with 10,882,755,219 streams worldwide'
return Pop
and the file for where the user will put input
if choice=='W':
print(top10songs.Mostpopularw(Pop))
the code runs fine but when I try to enter 'W; it prints out NameError: name 'Pop' is not defined but I dont understand how pop is not defined? Can anyone help me? Thanks!
Solution 1:[1]
It is not very clear what you want your Mostpopularw
function to do, so its hard for us to help you make it do that thing. The current code doesn't make much sense, as you're comparing a Pop
argument with a constant string, and then throwing away the result of the comparison before returning the same Pop
value.
It may be that you just want the function to return
the string, in which case it shouldn't be an argument:
def Mostpopularw():
Pop = 'Pop with 10,882,755,219 streams worldwide' # note, one = sign here!
return Pop
Now the calling code doesn't need to pass in an argument for Pop
, which was the error you were having (you were passing in something that didn't exist).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Blckknght |