Apr-01-2019, 01:04 PM
Hello,
I am new in Python programming and started reading Python documentation. Few queries I would like to ask:
I tried to use it before assigning to it and it said:
"> UnboundLocalError: local variable 'c' referenced before assignment".
Another question I am trying to solve in beginner section of Codechef
Here is the snippet of code:
Could you please explain why it is of type list?
Any idea/help would be appreciated
Thanks.
I am new in Python programming and started reading Python documentation. Few queries I would like to ask:
def fib2(n): # return Fibonacci series up to n """Return a list containing the Fibonacci series up to n.""" result = [] #why required this? a, b = 0, 1 while a < n: result.append(a) # see below a, b = b, a+b return resultI'd like to know why it is required to initialize result=[]? However it was stated that it does not require to "declare" variable before assigning to it.
I tried to use it before assigning to it and it said:
"> UnboundLocalError: local variable 'c' referenced before assignment".
Another question I am trying to solve in beginner section of Codechef
Here is the snippet of code:
n,k = (input().split()) print(type(n),type(k)) #str type n= int(n) k=int(k) #int type print(type(n),type(k)) while(n): n=n-1 num = (input().split()) print(type(num)) #list type num = int(num) if(num/k==0): ctr=ctr+1 print(ctr)Initial print statements prints class str class str and class 'int' class 'int'. However, the print inside the while statement prints class 'list'.
Could you please explain why it is of type list?
Any idea/help would be appreciated
Thanks.