0
I want to print all the value of the itération not only the last
i=0 while i!=5: i+=1 Print(i) After writing this algorithme Then when I write print(i) they give me the last value of i I want them to give me all his value
15 Réponses
+ 1
As mentioned above, this is a syntax error.
The print(i) statement must be in the same indentation (4 spaces is default) as the rest in the while block.
Edit:
For your second question:
You could add ot to a list.
But if it show the result in one line depend on the number of items and your screen.
Be careful.
in THIS case
i = 0
result_list = []
while i != 5:
i += 1
result_list.append(i)
print(result_list)
>>>[1,2,4,5]
You will lose the 0.
If you change line 4 and 5 to:
i = 0
result_list = []
while i != 5:
result_list.append(i)
i += 1
print(result_list)
The 0 (or first item) stay in results.
>>> [0,1,2,3,4,5]
+ 3
iren yeger
https://code.sololearn.com/cA16YT8ej6R7/?ref=app
See the code...it runs fine
Maybe you have done improper indentation.
Also make sure the p in print is lower case.
+ 2
Well you have some syntax errors. The correct code would be-
i=0
while i!=5:
print (i)
i+=1
+ 2
Write the print statement inside the loop with proper indentation.
+ 2
Also to get the iteration on a same line with the function print(), use it like this:
print(i, end=" ")
Look this example:
https://code.sololearn.com/cBDnZlKJsVWA/?ref=app
Try to erase end=" " to see the difference.
+ 2
Thank you very much Mathieu D. and Aditya Raj
for your inputs.
I learned something new.
I will workout arr, posted by Aditya and I will workout the very interesting solution by Mathieu.
How did you come to this, Mathieu?
Edit:
I got it for myself.
print() is a function.
The (build- in - pre definined) arguments of the print function are the following ones:
def print(value,
sep=' ',
end='\n',
file=sys.stdout,
flush=False)
The print function can print an arbitrary number of values ("value1, value2, ..."), which are separated by commas. These values are separated by blanks.
Source:
https://www.python-course.eu/python3_print.php
What he has done is, he redefined one of the build in standard arguments of the print() function.
Maybe it helps somebody.
+ 1
Hi iren yeger
Please put proper heading of your question for others to understand it easily. ☺
+ 1
Sachin Artani done
+ 1
I wrote the code.
I explained the indentation.
Did you read the code of Aditya?
Did you read my 2 codes?
Read them again.
Workout the differences.
In the code of Aditya is no list.
Did you read what I wrote?
In the my first code for you id no list.
Please read all again. Read it carefuly.
Type the code in a interpreter.
Look at the differences between the 2 codes which I shown.
+ 1
According to your expected output, the code is:
https://code.sololearn.com/csaeD3WsJfKA/?ref=app
+ 1
Thank You it work
Sven_m
0
Aditya Raj it IS thé same thing they only print thé last value
I want when I write print(i) on thé shell, they print all thé value of the itération on thé same line
0
Aditya Raj
I want to have the result as this
[0 1 2 3 4]
All thé résults writen on the same line
0
I think that it IS the same as thé example of Aditya Raj
Please can you try to write it
Sven_m