我正在构建一个python脚本,用于在上传到internet时查找文件的ETA。我在python上做的很粗糙。 基本上程序要求你输入文件大小和上传速度。 但我卡住了!!
剧本是这样的:。。(进行中)
print "UPLOADING TIME COVERTER"
print
print " Please enter the file size in mb"
size = raw_input()
# for e.g 237mb
print
print "Please enter the current uploading speed in kb /sec"
speed = raw_input()
# for e.g 60kb/sec
print
A = speed * 60
# speed is changed into minutes
B = (A / 1024)
#KB is changed in MB
C = (B / size)
print "your eta is %r hours" %(C)`
我收到的错误是:
Traceback (most recent call last):
File "uploadingeta.py", line 14, in <module>
B = (A - 1024)
TypeError: unsupported operand type(s) for -: 'str' and 'int'.
我到处搜索谷歌,但都找不到解决办法。
不能将str和int分开。必须将其转换为int,例如
int(速度)
您还可以向文件中添加用于调试的代码,例如
打印(类型(速度))
这将告诉您变量的数据类型,它对解决各种问题都很有用。
使用
speed = int(raw_input)
代码出错的原因是,当使用原始输入时,它将输入记录为字符串,因此在本例中,必须使用int()对其进行强制转换。
你可能已经看到了其他的答案,但希望我的能解释一下这个问题。
这里有一些代码来进行解析和阅读。修改以适应。
相关问题 更多 >
编程相关推荐