打印后将cmd光标保持在同一行

时间:2018-01-07 20:09:06

标签: python windows input cmd

我正在制作一个在命令提示符下运行的游戏,它应该在提示后接受输入。但是,我需要光标与提示保持在同一行。像“输入下一步:[光标停留在这里]”之类的东西。即时通讯使用msvcrt获取输入。

def getinput(game):
display(game)
inp = 21
print("Input next move: ")
while True:
    if msvcrt.kbhit():
        inp = int(msvcrt.getch().decode("UTF-8"))
        while (inp > 8 or inp <0):
            print("Choose a number between 0 and 8")
            inp = int(msvcrt.getch().decode("UTF-8"))
        while (listgame[inp] != "."):
            print("Already occupied. Choose an available space")
            inp = int(msvcrt.getch().decode("UTF-8"))
        break
game[inp]=enemy
print("You played: " + str(inp))

1 个答案:

答案 0 :(得分:0)

您可以使用print语句中的end选项阻止它转到下一行,如下所示:

 print("Input next move: ", end="")

这会将光标保持在行的末尾。