【问题标题】:How to multiply a string by a variable integer in python如何在python中将字符串乘以变量整数
【发布时间】:2015-09-23 23:31:41
【问题描述】:

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

gapVal=(" ")

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

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

【问题讨论】:

  • remove () ,否则你正在处理元组

标签: python string variables printing integer


【解决方案1】:

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

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, "*")

【讨论】:

  • 谢谢你,你帮了很多忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
  • 2015-01-08
  • 2013-10-29
相关资源
最近更新 更多