Python: why can a variable stand on its own? - python-3.x

I am a beginner in python told my friend today, that the following code would throw an error, but it did not:
a = 5
a
So I wondered, what does "a" actually do and why is the interpreter fine with this?
If this is a duplicate, please refer me to the right post and sorry in advance.
edit: I used a *.py file.

If you type this code into the shell and click enter, the value of a is returned. Functionally, as there is no operation being performed on a, the value of a will not change.
You define the variable in the line above. The variable contains a value, so the "NameError: name 'a' is not defined" error is not triggered.
Also, even if the variable is a different data type, for example, a string, the value of a is returned.
If you run the code in a different environment, the line won't be printed and the line won't impact the value of itself or of any other variables.

I think you tried it in der REPL Console, paste it to a *.py file and execute that. So when you just type the variable name and hit enter this is actually a print command behind the scenes

You can type in a int into the shell or whatever, and it will return it. The variable you put is just a int, so it returns 5.

Related

Corverting to Integer in Power Automate

I was searching for solution one by another and still have some problems, i am open to change even a whole schema, but i have time till weekend. If you have new solution show me, if i did something wrong please tell me.
This is where it happens
Also earlier i had problem with just initializing variable and puting there value from excel, but now i guess i am on right way. Earlier program told me that i am setting string to my int variable.
Yep, so i did that but still i receive:
InvalidTemplate. Unable to process template language expressions in
action 'var_name' inputs at line '0' and column '0': 'The template
language function 'int' was invoked with a parameter that is not
valid. The value cannot be converted to the target type.'.
I don't know what it meens and what to do, what i know is that i can still see and show what i got from my excel, also in excel file every cell i get is set to be Integer.
Can someone help me? I can show You more if its necessary.
I changed type of cells to intiger in my excel, also i made this function, and still none. Help, i do not have time and i worked on it for too long.

what went wrong with my Variable banner program?

I've started work on a Variable banner program, and I've hit a "phantom" syntax error.
name = input('Type here: ')
namelist = list(namelist)
print(namelist)
length_of_name=len(namelist)
asterisk=('*')
for length_of_name:
print (asterisk)
it throws up a syntax error, as I mentioned, but can anyone spot what I did wrong?
The SyntaxError is coming from your for statement. A for statement has to look like for variable in sequence_object:. Here sequence_object is any type that can be iterated, such as a list or tuple, but in your particular case for i in range(0,length_of_name): will make the code syntactically correct. But don't use this because there is a faster way which makes exactly the same output.
Instead of printing a single character in a loop, set asterisk to '*\n' and use print(asterisk*length_of_name, end=''). This prints the same output as the for loop but this multiplies the asterisk character by an integer to make a string that is repeated that many times. That way, you only print once. Setting end to an empty string ensures that a blank line is not printed.

Python3.4 Anaconda: Input() Function Broken?

I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.

Executing functions stored in a string

Lets say that there is a function in my Delphi app:
MsgBox
and there is a string which has MsgBox in it.
I know what most of you are going to say is that its possible, but I think it is possible because I opened the compiled exe(compiled using delphi XE2) using a Resource Editor, and that resource editor was built for Delphi. In that, I could see most of the code I wrote, as I wrote it. So since the variables names, function names etc aren't changed during compile, there should a way to execute the functions from a string, but how? Any help will be appreciated.
EDIT:
What I want to do is to create a simple interpreter/scripting engine. And this is how its supposed to work:
There are two files, scr.txt and arg.txt
scr.txt contains:
msg_show
0
arg.txt contains:
"Message"
And now let me explain what that 0 is:
First, scr.txt's first line is function name
second line tells that at which line its arguments are in the arg.txt, i.e 0 tells that "Message" is the argument for msg_show.
I hope my question is now clear.
I want to make a simple scripting engine.
In order to execute arbitrary code stored as text, you need a compiler or an interpreter. Either you need to write one yourself, or embed one that already exists. Realistically, the latter option is your best option. There are a number available but in my view it's hard to look past dwscript.
I think I've already solved my problem! The answer is in this question's first answer.
EDIT:
But with that, as for a workaround of the problem mentioned in first comment, I have a very easy solution.
You don't need to pass all the arguments/parameters to it. Just take my example:
You have two files, as mentioned in the question. Now you need to execute the files. It is as simple as that:
read the first line of scr.txt
check if it's a function. If not, skip the line
If yes, read the next line which tells the index where it's arguments are in arg.txt
pass on the index(an integer) to the "Call" function.
Now to the function which has to be executed, it should know how many arguments it needs. i.e 2
Lets say that the function is "Sum(a,b : integer)".It needs 2 arguments
Now let the function read the two arguments from arg.txt.
And its done!
I hope it will help you all.
And I can get some rep :)

Same for loop, giving out two different results using .write()

this is my first time asking a question so let me know if I am doing something wrong (post wise)
I am trying to create a function that writes into a .txt but i seem to get two very different results between calling it from within a module, and writing the same loop in the shell directly. The code is as follows:
def function(para1, para2): #para1 is a string that i am searching for within para2. para2 is a list of strings
with open("str" + para1 +".txt", 'a'. encoding = 'utf-8') as file:
#opens a file with certain naming convention
n = 0
for word in para2:
if word == para1:
file.write(para2[n-1]+'\n')
print(para2[n-1]) #intentionally included as part of debugging
n+=1
function("targetstr". targettext)
#target str is the phrase I am looking for, targettext is the tokenized text I am
#looking through. this is in the form of a list of strings, that is the output of
#another function, and has already been 'declared' as a variable
when I define this function in the shell, I get the correct words appearing. However, when i call this same function through a module(in the shell), nothing appears in the shell, and the text file shows a bunch of numbers (eg: 's93161), and no new lines.
I have even gone to the extent of including a print statement right after declaration of the function in the module, and commented everything but the print statement, and yet nothing appears in the shell when I call it. However, the numbers still appear in the text file.
I am guessing that there is a problem with how I have defined the parameters or how i cam inputting the parameters when I call the function.
As a reference, here is the desired output:
‘She
Ashley
there
Kitty
Coates
‘Let
let
that
PS: Sorry if this is not very clear as I have very limited knowledge on speaking python
I have found the solution to issue. Turns out that I need to close the shell and restart everything before the compiler recognizes the changes made to the function in the module. Thanks to those who took a look at the issue, and those who tried to help.

Resources