猿问

为什么 python 似乎会乱序执行代码?

执行时,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:

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


茅侃侃
浏览 108回答 2
2回答

跃然一笑

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

狐的传说

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):&nbsp; &nbsp;tempstring = "Color"+str(x+1)+":"&nbsp; &nbsp;guess[x]=input(tempstring)输出Python 3.7.4 (default, Jul&nbsp; 9 2019, 00:06:43)[GCC 6.3.0 20170516] on linuxGuess the codeColor1:
随时随地看视频慕课网APP

相关分类

Python
我要回答