如何将字符串乘以 python 中的可变整数

How to multiply a string by a variable integer in python

我在将字符串乘以整数变量时遇到问题。整数是变量形式,因为它是用户输入的输入。但是我不能让它们相乘,所以我可以打印它们。程序符合并运行,但在遇到它们相乘的代码时遇到语法错误。

gapVal=(" ")

gapSp=int(input("Please enter a the amount of spaces between the stars"))
gapSp2=(gapSp)-2
gapSp3=(gapSp2)-2

print("*", gapSp, "*", gapSp2, "*", gapSp3, "*")

不过我明白你在做什么,你想要这样的代码吗?

gapVal = " "

gapSp=int(input("Please enter a the amount of spaces between the stars"))
gapSp2 = gapSp - 2
gapSp3 = gapSp2 - 2

print("*", gapSp * gapVal,
      "*", gapSp2 * gapVal,
      "*", gapSp3 * gapVal, "*")