Output line number of running python program [duplicate] - python

This question already has an answer here:
Can I trace all the functions/methods executing in a python script?
(1 answer)
Closed 5 years ago.
Is there a way to output line number of python program when running?
e.g., given the following program
if False: #line 1
print("hello") #line 2
else: #line 3
print("hello1")#line 4
It will output 1,3,4 after execution of the program.

I didn't fully understand your question, but I think this'll answer it.
Try checking out this link: https://docs.python.org/2/library/trace.html
And go down to section 26.7.1.1.
I believe that the -c --count command is what you're looking for, or the -t --trace.

Related

How to comment out a chunk of a single line of code in python? [duplicate]

This question already has answers here:
Mid-line comment in Python?
(2 answers)
Closed 1 year ago.
In languages like PHP we can do the following:
>>> echo "cat is"." not"." dead"
cat is not dead
>>> echo "cat is"/*." not"*/." dead"
cat is dead
What I have done on the second line is, commenting out a chunk of it using PHP's multi-line comment feature.
Please let me know how to do the same in Python without breaking the line of code into separate lines.
Thanks..!
Generally, no. To keep old code around, duplicate the line, comment out one, and modify the other.

"EOF when reading a line" Python error [duplicate]

This question already has answers here:
Python 3: EOF when reading a line (Sublime Text 2 is angry)
(5 answers)
Closed 5 years ago.
name = input('Hey what is your name?: ')
print('nice to meet you ',name)
Whenever I try to run this I receive the following error :
"EOF when reading a line".
Could anyone help me with this?
Just a guess: you need to run this in a terminal (it does work when run like this). I am thinking that your code does not have any chance to get any input (hence the End Of File warning).

Removing printed lines in Python 3 [duplicate]

This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().

Repeat a set of code, until something is achieved - Python [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I have a program that gets a string from a user (raw_input) and sees if the length of it is greater than 8, for example. If it isn't it starts again, otherwise the program ends.
I'm using Python 2.7.8 and this is the code I'm using so far, but it doesn't work...
password = raw_input("Type in a password: ")
if password.len() > 8 :
print "Successful!"
else :
goto(1)
Can anyone solve this problem?
You need to learn the basics. You should start with a Python tutorial.
Here's the official Python tutorial from python.org; this section introduces the while statement, which you can use to make a loop.
https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming

Syntax error its there but where? [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
Once again Python's helpful error messages make me eat my keyboard.
I've checked the whole script forwards and backwards but can't find any "syntax error(s)".
Is there a decent debugger for Python or a helpful website which is able to scan my code for errors?
C:\Users\Daapii\Desktop>"foo.py"
File "C:\Users\Daapii\Desktop\foo.py", line 26
print "test"
^
SyntaxError: invalid syntax
Python why u no tell me more!
If this is Python 3, then the print function now requires parenthesis. Try print("test").
(EDIT: If Python continues to make you want to eat your keyboard, this might help.)
print is a function in Python 3:
print ("test")
Find other changes here: docs

Categories

Resources