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.
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!
I'm new to Python and have been working through some tutorials to try to get to grips with different aspects of programming.
I'm stuck on an exercise that is most likely very simple however I am unable to find the solution.
How do I create a program that reads one line of input and prints out the same line two times?
For example if the input was Echo it would print:
Echo
Echo
Any help with this would be hugely appreciated. I think I'm making a simple logic error but don't yet have the skills in place to recognise what it is.
The other answers seem logical enough, but what if you wanted to print it let's say a 1000 times or a million times? Are you really going to be typing print(variable) a million types? Here is a faster way:
j=input("Enter anything.")
for i in range(2):
print(j)
Here, I can change the value of range to whatever I want, and J will be printed that many times.
What happens here, is that the variable i loops upwards (an increment) to the number 2, so to explain it to a beginner, i travels t=from number to number. Where I put print(j) for every number i loops through until it gets to 2, J will be printed.
It sounds like you've been doing the input and output in one go:
print(input())
That works for doing a single echo of the input, but makes it a bit harder to repeat the same thing twice. An easy workaround would be to save the inputted text to a variable, which you can print twice:
text = input()
print(text)
print(text)
If you needed to do the input and doubled output with a single statement, you could use string formatting to duplicate the text with a newline in the middle:
print("{0}\n{0}".format(input()))
way complex right?(:D)
inp = input("Input something would ya? ")
print(inp)
print(inp)
Hi I am learning python by doing the practice problems for Open course at MIT 6.00 Intro to Computer Science.
I am Trying to do practice problem 1 part 2 create a recursive function to count the instance of key in target. My code so far...
from string import *
def countSubStringMatchRecursive (target, key,x,s):
if (find(target,key)==find(target,key,s)) and (find(target,key)==find(target,key,(find(target,key)))):#if first and last
return (1)
elif (find(target,key)==find(target,key,s))and (find(target,key)!=find(target,key,(find(target,key)))):#if first but not last
x=1
s= find(target,key)
return (countSubStringMatchRecursive(target,key,s,x)
elif (find(target,key,s))==-1 and (find(target,key)!=find(target,key,s)):#if last but not first
return (x+1)
elif:(find(target,key,s))!=-1 and (find(target,key)!=find(target,key,s)):#if not last and not first
x=x+1
s= find(target,key,s)
return (countSubStringMatchRecursive(target,key,s,x)
I getting a syntax error at line 8. I would just like to know what I did wrong there. Dont worry about the other mistakes I should be able to get those sorted out. I just Stuck on this. Thanks.
You're missing a closing parenthesis in line 8 and in the last line. Actually, the corresponding opening parenthesis is unnecessary, you might as well rewrite those line as this:
return countSubStringMatchRecursive(target,key,s,x)
Also, as pointed by #rodion in the comments, the last elif has a misplaced :, remove the one right before the opening parenthesis.
And talking about parenthesis: in Python you don't have to place the conditions of an if ... elif ... else statement inside parenthesis, you should drop them.
You have an unmatched paren on line 8. Add another close paren at the end
return (countSubStringMatchRecursive(target,key,s,x))
Same thing applies to your final return statement.
You have a mismatched parenthesis. Remove the ( near the beginning of the line or add a ) to the end.
return (countSubStringMatchRecursive(target,key,s,x)
You're missing a closing ). But you didn't need the opening one either; the syntax of a return statement is return <expr>, so anything that's a valid expression can directly follow return. Anything that's a valid expression is still a valid expression meaning exactly the same thing if you surround it with parentheses, but mostly that just adds noise when it's not needed.
There are several problems:
You are missing a closing paren on line 8
the body of your function needs to be indented. (update: has been fixed)
Also, your last elif has a : right after it, that needs to be
deleted. I.e.,
elif: expression:
should be
elif expression:
Your last statement is also missing a closing paren
return (countSubStringMatchRecursive(target,key,s,x)
should be
return (countSubStringMatchRecursive(target,key,s,x))
Finally #drewk's recommendation about taking a look at the PEP 8 is a good one. I periodically go back and review it myself.
Also, you have a number of unnecessary ()s .. they don't do any harm, but they are not needed and may reduce the readability of your code.
I am teaching myself python. I was thinking of small programs, and came up with an idea to do a keno number generator. For any who don't know, you can pick 4-12 numbers, ranged 1-80, to match. So the first is part asks how many numbers, the second generates them. I came up with
x = raw_input('How many numbers do you want to play?')
for i in x:
random.randrange(1,81)
print i
Which doesn't work, it prints x. So I am wondering the best way to do this. Make a random.randrange function? And how do i call it x times based on user input.
As always, thank you in advance for the help
This should do what you want:
x = raw_input('How many numbers do you want to play?')
for i in xrange(int(x)):
print random.randrange(1,81)
In Python indentation matters. It is the way it knows when you're in a specific block of code. So basically we use the xrange function to create a range to loop through (we call int on x because it expects an integer while raw_input returns a string). We then print the randrange return value inside the for block.