+ 2
I wrote a new def func code with two arguments but it gives error . i inserted the code here. can any one tell me the problems?
5 Réponses
+ 7
# This is your code:
def python(list, loop):
list=[12,14,16,['foo']]
while (len(list)) == 4:
return list
loop=[24,13,'Ben']
while true:
return loop
python(list, loop)
#This is the error inPyDroid 3 on Android:
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 9, in <module>
NameError: name 'loop' is not defined
Here, the scope of list and loop variables are only within the python function definition. When you call the function, a stack frame is created. list and loop are initialized in that frame. Outside the frame the variables don't exist.
So, when you call the python() function from outside its definition, the variables list and loop aren't defined. That's why you get errors.
+ 6
To display any output, you need to use stdout. Python uses stdout through print() function in Python 3. In order to print the value returned by method(),
print(method(blah))
can be used. So, wrap your call to python() function in a print() statement.
+ 2
DAB Ok thank you so much for explanation. But it gives no output.
What else do i need to do?
+ 1
you don't need those arguements
remove them as you don't need to pass anything into function
and assign function to a varianble to get it's return value
0
'''
list and loop arguements are literally useless
you are defining everything in function
parameters are used no where
'''
def python():
list=[12,14,16,['foo']]
while (len(list)) == 4:
return list
loop=[24,13,'Ben']
while true:
return loop
print (python())