Finishing up the code and it's all good and even working, it's just that in the python script, some loops use 4 tabs (!) as one indentation, others the usual 1 tab. I can't figure out what's the problem ? (in the code, the only difference is that some loops are thus much further extended to the right, given that for each loop, four tabs are placed before the first character of a new line, each time a line is finished with a return).
Example :
Normal:
for x in range(5):
print (x)
But sometimes:
for x in range(5):
print(x)
The difference in between these two examples is sth that is not done by myself; on both loops, all I do is press the return key after the semicolon. Nothing changes, even if I delete the print part and rewrite it with a new return key hit after going back to the semicolon.
Related
I want to ovewrite 2 separate print lines in while True loop. I tried \r and \n but it is not working. When using \r \n override only 1 line or 2 line prints continously.how i fix this?
import sys
import time
tot = 5
new = 2
while True:
sys.stdout.write('{0}\n\r'.format(tot))
sys.stdout.write('{0}\n\r'.format(new))
sys.stdout.flush()
time.sleep(1)
tot += 1
new += 1
\r brings you to the beginning of the line where you will start overwriting stuff.
\n adds a newline
\n\r takes you to the next line, and then bring you to the beginning of that line, which is already a blank new line, and so the \r isn't actually doing anything and this is the same as just doing \n.
If you only wish to overwrite a single line you can do this with \r followed by your text. Make sure that whatever you print out has enough spaces at the end of it to make it the same length as the line you are overwriting, otherwise, you'll end up with part of the line not getting overwritten.
But since you've clarified that you need to overwrite multiple lines, things are a little bit trickier. Carriage returns (\r) were originally created to help with problems early printers had (This Wikipedia article explains these problems). There is a completely different mechanism for overwriting multiple lines that isn't exposed natively by Python, but this Stack Overflow question lists a few 3rd party libraries you can install that will help you accomplish this.
To overwrite the previous line, you need to have not yet output a newline. So I don't think you can overwrite multiple lines, sadly. To do one though, try something like this:
while some_condition:
sys.stdout.write("\r{0}".format("your text here"))
sys.stdout.flush()
The \r here (called a "carriage return") moves the printing cursor back to the beginning of the line (ie, to right after the most recent newline \n). You cannot go back farther than that, though.
Okay so I was trying out the Google Kick Start problems for the first time when I came across this problem. For some reason when I enter multiple lines of inputs at the same time and then print in between processing each input it makes the loop run forever until I enter in a new input. Is there a simple explanation for this scenario?
x = int(input())
print('\nx =', x)
for i in range(x):
newInput = input()
print(newInput)
If I put the input as:
3
1
2
3
Then the output is:
x = 3
1
2
and then I have to press 'enter' again to see
3
Why does the program require the 2nd input to get the remaining 3 to print?
Curiously when I use the debugger mode with breakpoints on my IDE this problem disappears (no longer need to press 'enter' again).
Python waits for a newline when you call input(). If you copy paste the input data without a newline at the end it doesn't know that the last line is complete, but the lines before that end in newlines so it does them. Make sure that what you copy paste contains a newline at the end.
I have to print something by taking input from a file. The first few lines are empty. Therefore the output is turning out to be empty. It's like someone has pressed enter key 10 times before writing anything.
I want to ignore those inputs and consider only those which are not empty. What should I do?
By checking if there is anything apart from a newline character ("\n")is present in a line, your problem can be solved
fileObj=open(Filename)
for row in fileObj:
if len(row.replace("\n",""))>0:
print (row)
#Do your operations
If you can edit your question to add material, that would be helpful, but here’s a few pointers for now.
Assuming you’re taking the file in as a string (let’s call it "f"), you can loop over empty lines with a while loop:
charN = 0
while f[charN] == “\n”:
f = f[1:]
This allows you to chop off only the initial returns while keeping any line breaks later on in the file.
Note that, depending on the system this was written in, the enters may be stored as “\r\n”, in which case you could easily alter this for loop to remove those characters too. Good luck!
How can I write this complete code in python in just one line or may be I must say something which uses least space or least no of characters?
t=int(input())
while t>0:
n=int(input())
s=sum(1/(2.0*i+1) for i in range(n))
print "%.15f"%s
t-=1
You're welcome
for t in range(int(input()), 0, -1): print '%.15f' % sum(1/(2.0*i+1) for i in range(int(input())))
EDIT (explanation):
Firstly, instead of a while loop you can use a for loop in a range. The last argument in the for loop is a -1 to subtract 1 every time instead of the default of plus 1 every time.
If there is only one statement in an if statement, or loop, you can keep the one statement in the same line without going to the next line.
Instead of creating the variable of n, you can simply plug it in since it's only being used once. Same goes for s.
for _ in range(input()):print"%.15f"%sum(1/(2.0*i+1)for i in range(input()))
exec"print sum((-1.)**i/(i-~i)for i in range(input()));"*input()
I know I am too late for answering this question.but above code gives same result.
It will get even more shorter. I am also finding ways to shorten it. #CodeGolf #Python2.4
I'm really new to programming (really, really new) and need help with the basics. I'm trying to write a program with python that will compare the contents of two .txt files, one a reference and the other the source. The contents are a simple random listing of names, and I want it to print out if there are any names in the source that are not in the reference.
I've looked at other stuff on this site but every time I tried it, the terminal would never actually give a result, even if there was a print command in the program.
I also have a hard time reading the language of a program and ascertaining it's exact function, so something with clear directions would be really appreciated.
As far as I have is:
ref = open("reference.txt")
sor = open("source.txt")
list1 = ref.read()
list2 = sor.read()
for i in list2:
if i != list:
print i
ref.close()
sor.close()
And when I try and run this, it says "expected an indented block"? at the 'print i' line. Why?
Please help me out, as I have to teach myself this stuff and am not doing too well.
Thanks.
If you are totally, completely new to programming then it will take you some time to be able to implement what you describe. Take a step back, pour yourself a beverage, and start here. Start at the beginning, and repeat each illustration until you understand.
http://docs.python.org/tutorial/
As previously mentioned, your inner if statement needs to be indented, as
for i in list2:
if i != list:
print i
This requires two indents because it is two nested blocks. As a basic rule of thumb, anywhere you're ending a line with a colon (:), you're starting a new code block, and should be indenting another level. This is so you can un-indent once to end the if block without ending the for block.
However, I doubt this will do what you want based on your description. It's likely you wanted something more like
sourceLines = set(sor.readLines())
for line in ref.readlines():
if line not in sourcelines:
print line
if blocks in python have to be indented, add another level of indent for your print i statement
for i in list2:
if i != list:
print i
These lines read the files as strings:
list1 = ref.read()
list2 = sor.read()
This loop iterates through the string one character at a time:
for i in list2:
This line compares the character to the list class:
if i != list:
I'll answer your indentation error first: you need another 4 spaces before the print statement. In Python indentation is important and you need to indent any block and dedent to end that block.
For your problem I am going to not give you written out code in advance but more of a flow on how to do it:
Create 2 sets (http://docs.python.org/library/stdtypes.html#set-types-set-frozenset)
Read both files into a seperate set (you can do this while iterating over a file and appending to your set).
Compare your two sets using the set1 - set2 syntax (see the link above) to show all items not common to both sets.
Hope you can make it work from this.
Now for the code:
with open('file1.txt') as file1:
set1 = set(line for line in file1)
with open('file2.txt') as file2:
set2 = set(line for line in file2)
print set1 - set2
This uses some principles you are probably not familiar with (look up: list comprehensions, generator comprehensions and the previously noted link about sets which are unique collections).