python print function parameteres for print in one single line [duplicate] - python

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 2 years ago.
import datetime
a = 100
b = 0
while a > b:
print(str(datetime.datetime.now()), end="\r")
b += 1
print(b)
I don't know why this doesn't work, i've searched a lot and everyone does this like this but it doesn't work for me, im confused!
What i want to achieve is to overwrite the print and print in the same line without making a ton of lines.
instead of this:
1
2
3
i wanted this:
1 and on the next print it would only appear 2 and next only 3 and etc..

previously answered but summarized here:
import sys
while a > b:
print('\r')
# delay for readability
print('your statement')
sys.stdout.flush()

You need this I believe! as per Python 3.x
Please read the document - here
Also, refer to this question - how to overwrite stdout
import datetime
a = 100
b = 0
while a > b:
print(str(datetime.datetime.now()), end="\r")
b += 1
print(b, end = '\r')
OR
for x in range(10):
print('{0}'.format(x), end = '\r')
print(end='\r')

Related

how to print random number 10 times in one line [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
I want to print 10 random number and I try this:
import random
number = 10
while number <= 10:
print(random.randint(0,9))
number += 1
but this print next number in the new line. I try below code to fix that but:
import random
number = 10
my_list = []
while number <= 10:
my_list.append(random.randint(0,9))
number += 1
print my_list
it display number with , and []
To print without a new line you should edit the end argument which is set to newline by default, in. You could edit the print command in your first attempt to:
print(random.randint(0,9), end = " ")
import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist.append(n)
print(randomlist)
you can read more about it [here][1].
for the basics of printing:https://www.geeksforgeeks.org/print-without-newline-python/

How to not print new_lines in a while loop? [duplicate]

This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
How to print without a newline or space
(26 answers)
Closed 2 years ago.
I have a while loop. The purpose of this while loop is to trivially decide if a given integer N exists at a given index location of M.
I can achieve a better space complexity if I only used one line for each request of an input. Arbitrarily giving one element at a time instead of an entire list.
M = 2
N = 2
index = -1
while True:
print('take input: ', end = "")
a = str(input())
index = index + 1
if int(a) == N:
if index == M:
print('yes')
break
if index > M:
print('no')
break
if index < M:
if int(a) == N:
print('no')
break
Unintended result
================= RESTART: C:\Users\User\Desktop\logspace(m).py ================
take input: 1
take input: 2
no
>>>
I would like the console to only do one line at a time as shown below.
================= RESTART: C:\Users\User\Desktop\logspace(m).py ================
take input: (Do not echo previous inputs only one integer at a time... All on one line!)
no
>>>
Question
Is there any functions, modules, etc that could do this without tinkering with my console settings?

In a for loop how to send the i few loops back upon a condition [duplicate]

This question already has answers here:
How to change index of a for loop?
(5 answers)
Closed 3 years ago.
is there any way to send the value of a for look back in few loops..
for i in range(10):
print(i)
if random.randint(0,9) in [5,6]:
i = 0
what i want to do is exactly something like this. now here i know the range(10) generates the range [0,1,2,3,4,5,6,7,8,9] first the then traverse it by assigning the value to i. so simply changing the value of i will not work.
but is there any other way to change i in a way that the value in the next loop will i+1
Instead of a for loop, you can do a while loop by declaring i outside the loop.
i = 0
while i < 10:
print(i)
if random.randint(0,9) in [5,6]:
i = 0
else:
i += 1
With this, and a bit of tweaking, I think you can get the result you wanted.
This will work
import random
i = 0
while i < 10:
print(i)
if random.randint(0,9) in [5,6]:
i = 0
else:
i +=1
Edit: my bad, it won't work indeed, while loop will.

Python IDLE less or more? [duplicate]

This question already has answers here:
Paging output from print statement
(3 answers)
Closed 5 years ago.
Getting a list of __builtins__ in IDLE how do I pause it after a certain number or bottom of the screen?
This gives me one at a time..
>>> for i in dir(__builtins__):
... print i
... raw_input("Press Enter...")
and I could slice it like ...
x=dir(__builtins__)
len(x)
for i in x[:10]:
print i
... and that give me first 10 but is there a way to get it to print 10, or bottom of screen until the user input ? Like a less or more in Unix?
Thanks!
Try something like:
print_every = 5
for i, f in enumerate(dir(__builtins__)):
print f
if i % print_every == 0 and i != 0:
raw_input("Press Enter...")
enumerate pairs each entry in the list with its index in the list
if i % print_every == 0: checks to see if i (the current index) is a multiple of print_every.
The above code should print the list in groups of print_every many entries.

How can I add 1 to the parameter [duplicate]

This question already has answers here:
Why can a function modify some arguments as perceived by the caller, but not others?
(13 answers)
Closed 6 years ago.
Im stuck with this very simple code were I'm trying to create a function that takes a parameter and adds 1 to the result and returns it but somehow this code gives me no results. (I've called the function to see if it works.)
Somebody please help me since I'm very new to python :)
def increment(num):
num += 1
a = int(input("Type a number "))
increment(a)`
I changed it to
def increment(num):
return num + 1
a = int(input("Type a number "))
increment(a)`
but still no results are showing after I enter a number, Does anybody know?
You need to return some value or it never will appear.
def increment(num):
return num + 1
a = int(input("Type a number "))
increment(a)
You need to ensure you return the num in the increment function and assign it to a.
Return:
def increment(num):
return num + 1
Assign:
a = increment(a)

Categories

Resources