【问题标题】:Unsupported operand all math symbols and floats?不支持的操作数所有数学符号和浮点数?
【发布时间】:2016-03-01 04:20:10
【问题描述】:

我得到的主要错误是;

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)

【问题讨论】:

  • 我点击了代码按钮,但它不起作用。如何编辑以尝试找出我做错了什么?没有编辑按钮吗?
  • 我认为你需要这样做:float(input(....))
  • 谢谢您的快速回复。现在可以了。

标签: python geany


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    fretum = float(scalelenth) / constnum 
    

    【讨论】:

      【解决方案3】:

      scalelength 是一个字符串(假设您使用的是 python3.x)并且str * float 不起作用(它会做什么?)

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

      fretum = float(scalelength)/ constnum
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 2013-10-07
        • 2018-10-29
        • 2015-08-04
        • 1970-01-01
        • 2014-11-23
        相关资源
        最近更新 更多