不支持的操作数所有数学符号和浮点?

2024-10-01 17:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我犯的主要错误是

TypeError:不支持/:“str”和“int”的操作数类型

#Name variables/imports. Ask usr for scale lengh and num of frets
import math

scalelenth = input ("please inter a number for scale \n > " ) 
constnum = 17.817

#Calculate Fret one distance 
fretum = scalelenth / constnum 

print("this is scale lenth %d " %scalelenth)

Tags: name类型forusr错误variablesimportsask
3条回答

scalelength是一个字符串(假设您使用的是python3.x),而str * float不起作用(它会做什么?)你知道吗

解决方法是将字符串转换为浮点:

fretum = float(scalelength)/ constnum

您需要先将字符串转换为float,然后进行计算:

fretum = float(scalelenth) / constnum 

在Python3中,input()返回一个字符串。您需要将其转换为数字:

scalelenth = int(input("Please enter a number for scale \n > "))

如果要包含小数,请使用float()

scalelenth = float(input("Please enter a number for scale \n > "))

相关问题 更多 >

    热门问题