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.
Related
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?
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')
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.
This question already has answers here:
Python: Problem with raw_input reading a number [duplicate]
(6 answers)
Closed 7 years ago.
A study drill at the end of the chapter asks me to make a function that calls a while loop that will count in an interval decided by the user, but I keep getting an endless loop. If I replace the x with a number, it will end, but if I leave it as x, as far as I can tell, it doesn't register the raw_input() from the user.
def counting_up():
i = 0
numbers = []
print "Where do you want to end?"
x = raw_input(">")
print "How much would you like to increment by?"
a = raw_input(">")
while i < x:
print "At the top, i is %d." % i
numbers.append(i)
i = i + a
print "Numbers now: ", numbers
print "At the bottom, i is %d." % i
print "Your numbers: ", numbers
for num in numbers:
print num
counting_up()
When you get x from the raw_input() function, it stores it as a string. Then when you try to compare the number i to the string x, it's not going to behave in the way it would if the 2 were numbers.
Try converting the variables to integers so that you can compare them as numbers.
while i < int(x):
#code
However, because you're now converting the input to an integer, your program will throw an error if the user's input isn't in the form of a number. You can either just assume they will give you the correct input, or do some error-checking like so:
x = raw_input(">")
try:
x = int(x)
except Exception as e:
print "You have to input a number!"
exit()
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
generate 10 random integers 1-100 store them in a list. Use a loop. Use a second loop to process the list. In this latter loop, display all numbers in the list and determine the sum of the odd numbers and the sum of the even numbers. Display these sums after the second loop has ended. what's wrong?
import random
randomList = [] # create list
sumEven= sumOdd = 0
for x in range(10):
r = random.randint(1,100)
print(r),
randomList.append(r)
for x in range(len(randomList)):
if (randomList[x]%2 == 0): #even number
sumEven += randomList[x]
else:
sumOdd += randomList[x]
print "\nSum of even numbers =",sumEven
print "Sum of odd numbers =",sumOdd
In the future, please post the full error message.
That being said, print is a function. You should use parentheses with it.
https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function