Skip to main content
 首页 » 编程设计

python之是否可以在一行中为变量赋值、打印和提供输入

2024年11月01日15shangdawei

我想让用户输入一个值,例如... Fruit 1 of 4: (input).. 但是,提示显示在下一行。

print("Fruit ", i+1, "of ", numF, ": ") 
selF = input() 

请您参考如下方法:

在input(..)中制作文本:

selF = input("Fruit %s of %s" % (i+1, numF)) 

或者使用:

selF = input("Fruit {0} of {1}".format(i+1, numF)) 

或者如果python版本高于或等于3.6,你可以使用:

selF = input(f"Fruit {i+1} of {numF}")