Python: cannot print (execution flow ?) - python

I am interested in a small python application, which can be downloaded here:
https://launchpad.net/treemap
If you run it, like this:
python treemap-basic.py examle-world-population.txt
It works just fine.
The problem is that even if I type a print command in the "treemap-basic.py" file:
print "Hello World !" # treemap-basic.py
I cannot see the message "Hello World !" at the Terminal. Why?

I downloaded this script, and inserted
print "Hello World"
on line 64. When simply trying ./treemap-basic.py on the terminal, you get an IndexError since treemap-basic.py expects a command line argument. When you specify a file to work on:
./treemap-basic examle-world-population.txt
You see a bunch of output in stout. If you scroll up to the top (right below where you first entered the command in the terminal) you should see "Hello World" as the first line of output.

Related

Function recall working in other IDEs but not VS Code

I wrote some very simple code:
def yo():
text = "hi there"
print(text)
print(text)
yo()
I ran this in Spyder and online compilers without error. Obviously it spits out:
hi there
hi there
But when I run this in VS Code terminal using the "Run Python file in terminal" play button I get
"SyntaxError: invalid syntax"
for line 1 (the def line).
When I type yo() into the terminal itself, I get the expected output of:
hi there
hi there
Why is do I get a different result from these? I executed other simple bits of Python in VS Code using the "play" button without issue. It goes without saying that I have the python extension and interpreter installed.
UPDATE: I restarted VS Code and now the file runs without issue. I guess "did you restart the computer" really does solve the issue sometimes...
Your function - yo(), is being defined, however Visual Studio Code does not know how to run it. To fix this, try adding the if __name__ == '__main__': clause. Here is your full code:
def yo():
text = "hi there"
print(text)
print(text)
if __name__ == '__main__':
yo()
Here is some more information about if __name__ == '__main__':
If that doesn't fix it, you must have some formatting issues or some different settings of Visual Studio Code. You could do the following things.
Make sure you're running the right file
Delete all of the code and paste it in again
Reset your Visual Studio Code settings
Make sure your settings for Tab are 4 spaces.
Disable terminal.integrated.inheritEnv in Settings
If all else fails, try these:
You should use the exit() command in the terminal to end python session. Then re-run and see if anything works.
Run your code using 'Start without debugging'.

Python script not really executing

I'm using the latest spyder/anaconda (Python 3.8) software and a beginner here. When running a simple hello.py script. I get runfile('C:/Users/Raj/CODE/Python Scripts/hello.py', wdir='C:/Users/Raj/CODE/Python Scripts') but nothing else in the console or anywhere saying "Hello World".
def hello():
"""Print "Hello World" and return None."""
print("Hello World")
It can do other print type scripts just fine as I tried an old script from college.
Any function needs to be called in order to get executed. You call a function by typing it's name with the (), in your case hello()
on bottom of the python file add the call to the function hello
hello()

Invalid syntax error in first python programming

I made my first python program of hello world.
Every time i save it,it cannot be open in the program.
And the program just blink and flashes away.
I read few line 'invalid syntax'.
here what I WROTE:
>>>print('hello world')
then i saved it as p.py and try to run it on program and zilch.
I have a feeling you are reading the book 'The self taught programmer'? What you need to do is realize the shell and the notepad are two different things.
Do this.
Open up the IDLE shell, but then go to the file-new-new file
This should open a new window. Now this window, simply put the Print("Hello, taxation is theft"). Now, save this window, as whatever you want. Now, in the same window, with both the shell open, and this new window, click F5 or run 'run module'.
You'll see what you made in the notepad will run on the shell window. It will only run once. The next step is to practice running it 5 times, then 100 times. You'll get there bud.
While saving .py file you must have beeen saving it as it is, containing other whitespaces and charater like ">>". remove ">>> " fr
In python 2.7 print looks like this:
print "hello world"
In python 3 it looks like this:
print( "hello world" )
Run your code with python3:
python3 p.py

Is there a way to replace a previously outputted text in Python?

There is something I want to do, but unfortunately I have absolutely no idea on how to do it, and I doubt someone else has asked. So, basically, what I want is the program to output text in an previous area. I'll try to explain.
Let's say I outputted the following text to the screen:
===========================================================================================
===========================================================================================
My question is, is there a way, without using pygame, to replace the blank lines between the lines with a certain text WITHOUT printing the lines again? Is this possible using for example Python IDLE or directly in the terminal, or is it only possible using pygame or something "like" that?
It would look like this, for example:
===========================================================================================
Hello World!
===========================================================================================
You can use the curses library for this kind of text-only UI. Here's a simple example, first printing the horizontal lines, and then, after some time, adding a string on a previous line.
import time, curses
scr = curses.initscr()
scr.addstr(0, 0, "#" * 80)
scr.addstr(2, 0, "#" * 80)
scr.refresh()
time.sleep(1)
scr.addstr(1, 35, "Hello World")
scr.refresh()
While there should be multiple libraries to handle this task, you should check out curses and colorama. I'm unsure about curses but this is definitely possible with colorama (install it via pip).
Here's an example:
from colorama import *
def pos(x, y):
return '\x1b['+str(y)+';'+str(x)+'H'
def display():
init() #just for safety here; needed in Windows
print(Fore.RED+pos(30, 10)+ 'This string is a different place!')
display()
Given your output, say, for example that is in a file test.dat:
===========================================================================================
===========================================================================================
a 1 simple line in vim would produce your desired output:
:3s/^$/Hello World!
If you want to automate this in a script: hello.sh
#!/bin/bash
ex test.dat <<-EOF
:3s/^$/Hello World!
wq " Update changes and quit.
EOF
Output:
===========================================================================================
Hello World!
===========================================================================================

How to see new results in console after updating imported module in PythonAnywhere

When I import a selfmade module and run the program, the output is what I expected. However when I update the module and run the program in the same console, the previous result is shown. If I open a new console, then the new result is correctly shown.
Let's take an example:
# Filename: myfunctions.py
def helloWorld():
print("Hello World")
# Filename: runfuction.py
from myfunctions import helloWorld
helloWorld()
The output is Hello World. When I replace in myfunctions.py Hello into Bye, and when I run the program in the same console, my result is still Hello World, and not Bye World. The updated text Bye World will be only shown when I open a new console.
try:
reload(module_name)
that's how it works in a local python console. I don't have a PythonAnywhere account, but I would guess it's pretty similar.
Note that any object instances you have already created will not be changed, but this (or something similar) should work fine for functions.

Categories

Resources