This question already has answers here:
Running python script in terminal, nothing prints or shows up - why?
(2 answers)
Closed 2 years ago.
At first I thought PythonScriptor was the problem, but I tried print('Hello World') and the output was fine.When ever I copy and paste a code it just gives me an empty line. I am currently learning Subroutines in Python, but I completely don't get it.
def myFirstSubroutine():
for i in range(1,3):
print('This is a subroutine')
The code snippet you just showed us defined the function. To execute the function, now you need to call it.
>>> myFirstSubroutine()
This is a subroutine
This is a subroutine
Thanks CoryKramer for his right answer. You need to call the function myFirstSubroutine() to get the output.
Pay attention to that lowercase letter variable is not equal to capital letter variable in Python. That means you need to use exact the function name myFirstSubroutine().
As well as 'I copied what you gave me , it doesn't work.', it can work if you really copy it instead of spelling the 'myfirstsubroutine' by yourself.
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 1 year ago.
Is it possible to run a String text?
Example:
str = "print(2+4)"
Something(str)
Output:
6
Basically turning a string into code, and then running it.
Use exec as it can dynamically execute code of python programs.
strr = "print(2+4)"
exec(strr)
>> 6
I will not recommend you to use exec because:
When you give your users the liberty to execute any piece of code with the Python exec() function, you give them a way to bend the rules.
What if you have access to the os module in your session and they borrow a command from that to run? Say you have imported os in your code.
Sure is, looks like your "Something" should be exec.
str = "print(2+4)"
exec(str)
Check out this previous question for more info:
How do I execute a string containing Python code in Python?
This question already has answers here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Python print immediately?
(1 answer)
Closed 2 years ago.
I have the following code:
for file_name, content in corpus.items():
print('here')
content = [list(filter(lambda index: index not in remove_indices, content))]
corpus[file_name] = np.array(content).astype(np.uint32)
Where corpus is a 800,000 long dictionary with string keys and array values.
Things were taking forever so I decided to check how fast each iteration was by adding in that print statement.
If I comment the last two lines out it prints lots of heres really fast, so there's no problem with my iterator. What's really weird is that when I uncomment the last two lines, here takes a long time to print, even for the first one! It's like the print statement is somehow aware of the lines that follow it.
I guess my question speaks for itself. I'm in Jupyter notebook, if that helps.
This question already has answers here:
Capture stdout from a script?
(11 answers)
Closed 3 years ago.
I'm trying to get a already written line in python 3, but I haven't found any function that can read a line from the terminal. It should work something like sys.stdout.read(), or sys.stdout.readline() but this function just throws an error.
If you mean to read from the user/a pipe, then simply use input.
However, from your comments it seems like you want to be able to read from what has already been printed.
To do this, you have a few options. If you don't actually want it to display on the terminal, and you only care about certain part of the output, then you can use contextlib.redirect_stdout and contextlib.redirect_stderr. You can combine this with io.StringIO to capture the output of your application to a string. This has been discussed in the question Capture stdout from a script in Python
However, if you want to have something which provides you both a means of printing to the terminal and giving you the lines, then you will need to implement your own type which inherits from io.TextIOBase or uses io.TextIOWrapper.
Do you mean something like this?
name = input("Enter a name: ")
print(name)
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
I am very new with python and programming altogether and have been trying to figure out how to do this for a while.
Here's what I need help with:
y=0
x=2
p01='hello'
p02='bye'
print('p'+(str(y)+str(x)))
The output is of course 'p02', but how can I make the output instead the value of p02 ('bye')
Hope this makes sense and I look forward to any answers.
You could use eval()...
It evaluates an expression stored in a string as if it were Python code.
In your case, 'p'+(str(y)+str(x)) becomes 'p01', so it gets the result of the expression p01, which is of course 'bye'.
print(eval('p'+(str(y)+str(x))))
Note however that you should never do this - there is almost always a better way. Please read Why is using 'eval' a bad practice?
So, what can we do?
globals() gives us a dictionary of all of the global variables in your Python program, with their name as a string index and their value as the dictionary value. Thus, we can simply do:
globals()['p'+(str(y)+str(x))]
Which evaluates to globals()['p01'], which gets the value of global p01 - which is bye.
Again, this is a workaround to a bigger problem
Restructure your code. Make them into an array or dictionary and get the index of it. Think through why you would want to do this, and change your code so that you do not have to. It is bad to be in a situation where eval looks like the best option.
This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 9 years ago.
I am trying to output text to stdout, overwriting the previous text, for example
for i in range(12):
print i
but with i replacing the previous value each time rather than appearing on a new line
From looking at quite a few previous posts with similar questions it seems that there are a few ways of doing this, possibly the simplest being (for Python 3.x on)
for i in range(12):
print(i,end="\r")
sometimes with a comma at the end of the print statement, sometimes not. However, without the comma I get no output at all and with the comma I get
(None,)
(None,)
(None,)
...
Is this something related to my terminal perhaps? I get similar results no matter which of the previous posted solutions to the problem I try.
Thanks for any help!
If you do this in a terminal you won't see any output because the for loop finishes too fast for you to see the output changing. After the loop is done the terminal's prompt overwrites the output.
Try this instead:
>>> for i in range(9999999):
... print(i, end="\r")
I'm surprprised you don't get a syntax error when omitting the comma.