1

执行时,input()提示在print()上面打印。执行的结果是打印

Color1:Guess the code

我正在使用树冠编辑器。我试过了guess[x]=input("Color %s:") % str(x+1),并且guess[x]=input("Color"+str(x+1))

guess=[None]*n
print("Guess the code")
for x in range(5):
   tempstring = "Color"+str(x+1)+":"
   guess[x]=input(tempstring)

我希望输出是

Guess the code
Color1:

然后允许用户输入一个字母

4

2 回答 2

1

当我复制并运行此代码时,它按预期工作。

对我来说,这看起来像是一个冲洗问题。您可以尝试flush=True在打印功能中强制刷新,如下所示:

print("Guess the code", flush=True)
于 2019-10-03T16:32:46.390 回答
-1

print("Guess the code")在代码行中添加“换行符”字符或您的操作系统等效项

print("Guess the code\n")

\n是 reg ex 中的新行

我相信您的操作系统正在将输出读入<Guess the Code>输入命令和打印输出的“标签”表达式。添加换行符会阻止操作系统认为它是标签并将其更改为 std 输出。


添加答案


如果删除第一行中的“星号 n”,代码可以在 linux 机器上运行:

guess=[None]
print("Guess the code")
for x in range(5):
   tempstring = "Color"+str(x+1)+":"
   guess[x]=input(tempstring)

输出

Python 3.7.4 (default, Jul  9 2019, 00:06:43)
[GCC 6.3.0 20170516] on linux
Guess the code
Color1:
于 2019-10-03T16:30:21.557 回答