Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
inputs = []
iterations = int(input())
for x in inputs:
currentInput = input()
inputs.append(currentInput)
print(x)
That code isn't working. It is supposed to make more "currentInput" variables based on "iterations". Thank you soooo, much, as this has been bugging me.
Your code isn't working because it's not right.
Your for loop is going through each element in the list inputs. But inputs is an empty list; you haven't added anything to it so the for loop won't work. You must have meant
for x in range(iterations):
currentInput=input()
inputs.append(currentInput)
print(currentInput)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2
I want to find all the solutions of a loop.
I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code:
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
print f2-f
else:
break
If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = []
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
results.append((f,S,S2,E,E2))
print f2-f
print "intermediate result:", results[-1]
else:
break
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to check if part of text is in tuple?
For example:
my_data = ((1234L,), (23456L,), (3333L,))
And we need to find if 123 or 1234 is in tuple.
I didn't worked lot with tuples before.
In array we use:
if variable in array
But its not working for tuples like my_data
PS. 1st answer solved problem.
def findIt(data, num):
num = str(num)
return any(num in str(i) for item in data for i in item)
data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)
Output
True
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm making a quiz and when I make an if statement in python to put a question I have to put the print before it(which I'm using at answers). So my question is how to make it come after without stopping the if statement. Any ideas appreciated. Sorry if its hard to know what I'm trying to say I'm a little new to python.
Something like this maybe?
answer = input ('Who was the last king of Rome? ') #use raw_input for python2.x
if answer == 'Tarquinius': print ('correct')
else: print ('incorrect')
print 'asdf' if 1 == 2 else 'fdsa'
Or if you don't get that this does, try this:
print "ok..nice" if raw_input("how are you ") == "ok" else "well, that's too bad"
Apparently people don't know the new inline if syntax, and think i'm trolling.
This should all be written in 1 line and does not require splitting.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert python codes to java. But I do not understand slicing step parameter.
Example:
x = "Hello World !"
x[6:2:-1]
And what is the result x[6:2:-1] ?
-1 step just reverts the direction of the slice:
>>> x = "Hello World !"
>>> x[6]
'W'
>>> x[2]
'l'
>>> x[6:2:-1]
'W ol'
[6:2:-1] means give me a slice from the 6th to the 2nd item (not including), reversed.
FYI, you don't need python installed for checking the result of the code you've asked about, go to pythonanywhere and play:
PythonAnywhere is a Python development and hosting environment that
displays in your web browser and runs on our servers.
Also see:
Explain Python's slice notation
Extended Slices