Skip to content
Advertisement

Tag: python-3.x

Referring to variables of a function outside the function

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

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:

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?

Advertisement