0
HELP PLEASE! Why am I receiving NameError 'c' is not defined? I've tried using c as a global variable but then it says NameErro
print ('What do you want to do?') a = input ('1. Take notes 2. Summarize notes: ') if a == ('1'): b = input ('Paste article or reading: ') c = b.split('.') def notes(): if c.len == 2 or 1: d = ''.join(c[1])
23 Antworten
+ 3
I'm in a hurry and I don't know if Kishalaya Saha already explained this, but you can't use "if len(c) == 2 or 1". 1 is true. Any expression "or true" is always true. You have to use if len(c) == 2 or len(c) == 1 instead
+ 3
Anna I usually do this:
'''
if len(c) in [1,2]:
#codes
'''
shorter, clearer, and easy to modify😀
+ 1
Hi NicoChis!
1. Looks like your code is incomplete. You are never calling the function notes(). It should be called somewhere after the function is defined.
2. I think c.len is not a valid syntax; it should be len(c).
3. I do believe writing
global c
after "def notes():" should fix the NameError. You may also pass c as an argument of the function notes, thus avoid using globals.
It may be better if you save the code in the Code Playground, and paste a link here.
+ 1
Kishalaya Saha thank you and also this isnf my full code or function i do end up calling the function after. but to clarify you are saying thati should use def notes(c)?
+ 1
Yes, that's one way of passing the list by reference to the function. We won't need globals then.
#define function
def notes(c):
#do stuff with c here
#call function
notes(c)
+ 1
Could you please paste a link to your full code?
+ 1
ok i just posted the link
+ 1
Kishalaya Saha OK I THINK I FIXED IT GIVE ME A SECOND
+ 1
Thanks!
So, if I try to run it with 1 as the first input, it works fine. But if I try 2, it doesn't work. Is that what's happening with you too?
Then the reason is simple: You have defined c only inside the block
if a == ('1'):
When a is not "1", c is not defined, and we get a NameError. To fix this I would call notes() inside the if block. That'd also require us to put the whole if block below the function definition. Does that make sense?
By the way, just if a == '1': suffices, you don't need the parentheses.
+ 1
Kishalaya Saha i changed input to raw_input and fixed the name error problem... and im glad it worked but can yoy explain why???
+ 1
Are you running the code here, or somewhere else with Python 2.x? raw_input() is for Python 2.x what input is for Python 3.x
The following is for Python 2.x:
raw_input() takes the argument as a string. So even if you enter 1, it would be treated as the string "1".
input() tries to evaluate what you enter. That is, when you enter 1, it would be interpreted as a number. Then to make it work, you'd have to do if a==1:, without the quotes around 1. Does that make sense?
+ 1
Kishalaya Saha just reaized why ive been making some mistakes ive ran my code on seperate things and assumed i was running python 3 on all of them. when i run the on my mac terminal i have been say python Notes.py instead of python3 Notes.py😭
+ 1
Kishalaya Saha i will thanks
+ 1
NicoChis the error with "c is not defined" happens when a!="1", because c is only defined when a=="1"
you should put notes(a) in the "if" block
and also, the whole "if" block should be at the end, because the definition of "notes(c)" should be in front of where it's used😀
0
Kishalaya Saha i was just trying that and it returned name error c is not defined again
0
Kishalaya Saha right now the code is only for taking notes so entering 2 does not have any purpose right now. i am gojng ot do that later
0
Great! Do you have any other question at the moment?
0
Ah yes, that's the problem! So everything is good now? :)
Let us know if you run into any more issues.
0
No, I didn't. Thanks Anna!