问题描述
因此,我正在尝试为Python中的俄罗斯方块制作一个PPS(每秒放置的零件)计算器。但是,当我尝试分割时间和碎片时,会出现此错误:
TypeError: can only concatenate str (not "int") to str
网站上的一些人说“将int转换为str!”之类的内容。 但是当我这样做时,会出现 this 错误:
TypeError: unsupported operand type(s) for /: 'str' and 'str'
代码如下:
pieces = input("How many pieces did you place? ")
time = input("How long did you play for? (In Seconds Please!) ")
PPS = " "
def answer():
PPS = round(str(time) / str(pieces))
print("Your PPS is: " + PPS)
answer()
请尽快提供帮助,谢谢!
解决方法
可以对数字执行数学运算,并在字符串上进行串联... 您可以尝试以下方法:
def answer():
PPS = round(int(time) / int(pieces))
print("Your PPS is: " + str(PPS))
或:
def answer():
PPS = round(int(time) / int(pieces))
print("Your PPS is: {0}".format(PPS))
或者您可以将int
包裹在input
周围
因此它变成pieces = int(input("How many pieces did you place? "))
,与time
类似
然后在函数内部:
pieces = int(input("How many pieces did you place? "))
time = int(input("How long did you play for? (In Seconds Please!) "))
PPS = ""
def answer():
PPS = round(time / pieces)
print("Your PPS is: " + str(PPS))
,
我已经解决了问题。我需要改变的问题
Time
和
Pieces
至
float(time)
和
float(pieces)
即可。