执行时,input()提示在print()上面打印。执行的结果是打印Color1:Guess the code我正在使用树冠编辑器。我试过了guess[x]=input("Color %s:") % str(x+1),并且guess[x]=input("Color"+str(x+1))guess=[None]*nprint("Guess the code")for x in range(5): tempstring = "Color"+str(x+1)+":" guess[x]=input(tempstring)我希望输出是Guess the codeColor1:然后允许用户输入一个字母
2 回答

跃然一笑
TA贡献1826条经验 获得超6个赞
当我复制并运行此代码时,它按预期工作。
对我来说,这看起来像是一个冲洗问题。您可以尝试flush=True
在打印功能中强制刷新,如下所示:
print("Guess the code", flush=True)

狐的传说
TA贡献1804条经验 获得超3个赞
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:
添加回答
举报
0/150
提交
取消