0

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?

Travis Wells
  • 321
  • 1
  • 2
  • 11
  • Does this answer your question? [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Chase Jul 11 '20 at 15:20
  • Does this answer your question? [Is there a way to clear your printed text in python?](https://stackoverflow.com/questions/19596750/is-there-a-way-to-clear-your-printed-text-in-python) – Daniel Geffen Jul 11 '20 at 15:20
  • @DanielGeffen It did not delete the last line. I suspect its the Console settings. – Travis Wells Jul 11 '20 at 15:26

0 Answers0