Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:/:';str';和';int';,。。。我想我把它改成了浮动_Python_Python 3.x - Fatal编程技术网

Python TypeError:/:';str';和';int';,。。。我想我把它改成了浮动

Python TypeError:/:';str';和';int';,。。。我想我把它改成了浮动,python,python-3.x,Python,Python 3.x,我是python新手,正在学习如何处理它,我试图创建一个程序,计算通过adb传输文件的时间,使用3mbps作为设置的传输速度,并首先将所有文件大小转换为字节(*1024*1024…)这是我的代码,以及它在底部产生的错误。 欢迎对代码进行任何改进 class AdbCalculator: file_size, size_units = input("Input the file size (MB or GB):\n").split() float(file_size)

我是python新手,正在学习如何处理它,
我试图创建一个程序,计算通过adb传输文件的时间,使用3mbps作为设置的传输速度,并首先将所有文件大小转换为字节(*1024*1024…)
这是我的代码,以及它在底部产生的错误。

欢迎对代码进行任何改进

  class AdbCalculator:
    file_size, size_units = input("Input the file size (MB or GB):\n").split()
    float(file_size)
    str(size_units)
    size_units.capitalize()
    time = 0.0
    speed = 3 * 1024 * 1024

    def sizeCalculator(self):
        # Change size to bytes
        if self.size_units == 'MB':
            self.file_size *= (1024 * 1024)
        elif self.size_units == 'GB':
            self.file_size *= (1024 * 1024 * 1024)
        else:
            print('Wrong size units! use GB or MB\n')
        return self.file_size

    def timeCalculator(self):
        # Calculating the time taken
        self.time = self.file_size / self.speed
        if self.time >= 60:
            foo = self.time / 60
            if foo <= 60:
                return 'About %s Minutes' % foo  # Time in minutes
            else:
                foo /= 60
                return 'About %s hours' % foo  # Time in Hours
        else:
            return 'About %s seconds' % self.time  # Time in seconds


foo = AdbCalculator()
print(foo.sizeCalculator())
print(foo.timeCalculator())
类ADB计算器:
文件大小,大小单位=输入(“输入文件大小(MB或GB):\n”).split()
浮动(文件大小)
str(尺寸单位)
大小单位。大写()
时间=0.0
速度=3*1024*1024
def尺寸计算器(自身):
#将大小更改为字节
如果self.size_units==“MB”:
self.file_size*=(1024*1024)
elif self.size_units==“GB”:
self.file_size*=(1024*1024*1024)
其他:
打印('大小单位错误!使用GB或MB\n')
返回self.file\u大小
def时间计算器(自身):
#计算所用时间
self.time=self.file\u大小/self.speed
如果self.time>=60:
foo=self.time/60
如果foo
float(文件大小)
此行不起作用。
您应该将其分配给自身,例如:
file\u size=float(file\u size

一些改进:

  • str(大小单位)
    也有同样的问题。它应该是这样的:
    size\u units=str(size\u units)
  • size\u units.capitalize()
    也有同样的问题。
    它应该类似于
    size\u units=size\u units.capitalize()

  • 你不能像那样把
    str
    转换成
    float
    。您应该执行
    file\u size=float(file\u size)
    。这就是当尝试
    self.file\u size/self.speed
    @Mr.E时错误所说的
    TypeError:str和int不支持的操作数类型worked@Mr.E你应该将此作为答案发布,这样才能被接受。
    
    222222222222...
     Traceback (most recent call last):
      File "/Users/User/Documents/Programming/python/python/adb time calculator.py", line 35, in <module>
        print(foo.timeCalculator())
      File "/Users/User/Documents/Programming/python/python/adb time calculator.py", line 21, in timeCalculator
        self.time = self.file_size / self.speed
    TypeError: unsupported operand type(s) for /: 'str' and 'int'
    
    Process finished with exit code 1