0

I'm making a script that show you a question, you give the answer ("y" or "n") and the line is restarted, and appears another question. I've tried this (assuming that the number is the question)

import sys

for i in range (10):  
    sys.stdout.write('\r'+str(i))
    label = input()
    if label=='n':
        doSomething
    if label=='y':
        doSomethingElse 

but in this cases, I get this

0n
1n
2y
3n

and I want that if I have

0

I give my answer and press Enter, the currently line disappear and the new number appears

1

and then I give an answer again, and so on.

I've already check this question and this. I'm using python 3.5.

Edit: Thanks to an answer, now I know that I need to avoid the '\n' of the input() function.

Alejo Bernardin
  • 1,105
  • 14
  • 22

2 Answers2

0

When you use input(),there always have a output with '\n'. This '\n' changed to a new line,then the '\r' can't work as you wanted.

So, Maybe you should break through here(the input()).

hxysayhi
  • 1,888
  • 18
  • 25
0

Maybe a \n like in (see Part 1) and you can use an elif (see Part 2)

for i in range (10):  
    sys.stdout.write(str(i) + '\n') # Part 1
    label = input()
    if label == 'n':
        doSomething
    elif label =='y':  # Part 2
        doSomething
Andy K
  • 4,944
  • 10
  • 53
  • 82