The result is: NameError: name ‘a’ is not defined Is it possible to use a outside the function? Answer In python the scope is a function, or class body, or module; whenever you have the assignment statement foo = bar, it creates a new variable (name) in the scope where the assignment statement is located (by default). A variable that
Tag: python-3.x
Access multiple elements of list knowing their index [duplicate]
This question already has answers here: Explicitly select items from a list or tuple (9 answers) Closed 7 months ago. I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5,
Uncrypting a very simple code in python
So this is the brief: i need to input something like: and return: As you can see to decode this i need to start by reading the words in reverse order and if the first letter of the word isupper() then make it lowercase and append it to a list which i will later print. This is what i have
Assign a range to a variable
Whenever I try to assign a range to a variable like so: Then try to print the variable: It simply prints ‘range(10, 50)’ instead of all the numbers in the range. Why is this? Answer Thats because range returns a range object in Python 3. Put it in list to make it do what you want:
Local variable referenced before assignment? [duplicate]
This question already has answers here: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use) (14 answers) Closed 6 months ago. I am using the PyQt library to take a screenshot of a webpage, then reading through a CSV file of different URLs. I am keeping a variable feed that incremements everytime
Simplest method to call a function from keypress in python(3)
I have a python application in which a function runs in a recursive loop and prints updated info to the terminal with each cycle around the loop, all is good until I try to stop this recursion. It does not stop until the terminal window is closed or the application is killed (control-c is pressed) however I am not satisfied
Convert range(r) to list of strings of length 2 in python
I just want to change a list (that I make using range(r)) to a list of strings, but if the length of the string is 1, tack a 0 on the front. I know how to turn the list into strings using but I want to be able to also change the length of those strings. Input: Output: And if
How to search and replace text in a file?
How do I search and replace text in a file using Python 3? Here is my code: Input file: When I search and replace ‘ram’ by ‘abcd’ in above input file, it works as a charm. But when I do it vice-versa i.e. replacing ‘abcd’ by ‘ram’, some junk characters are left at the end. Replacing ‘abcd’ by ‘ram’ Answer
Relative imports in Python 3
I want to import a function from another file in the same directory. Usually, one of the following works: …but the other one gives me one of these errors: Why is this? Answer unfortunately, this module needs to be inside the package, and it also needs to be runnable as a script, sometimes. Any idea how I could achieve that?
Python – sum of two numbers program error [duplicate]
This question already has answers here: How can I read inputs as numbers? (10 answers) Closed 4 months ago. I was just getting into Python programming. I wrote a simple program to calculate sum of two user-input numbers: Now if I enter the numbers as 23 and 52, what showed in the output is: What is wrong with my code?