在Python 3.3.0中使用用户输入进行划分可以使用但不能使用3.3.2

时间:2018-03-30 01:56:57

标签: python python-3.x

在python版本3.3.0中编写程序时(由于非管理员权限我无法更新)它完全正常,但是现在我使用的是python版本3.3.2它不再有效,程序如下:

num = input("please enter a value and press enter: ")

(假设选择1)

conv = input("what unit is the measurement in? cm, m, or km: ")

(假设选择cm)

if conv == "cm":
print("chosen value is cm, converting to m and km")
m = num/100
print(m , "meters")
km = num/100000
print(km , "kilometers")

所需的输出(见3.3.0):

please enter value and press enter: 1
your chosen value is:  1
what unit is the measurement in? cm, m, or km: cm
you have chosen cm, converting to m and km
0.01 meters
1e-05 kilometers

但我得到(在3.3.2中):

please enter value and press enter: 1
your chosen value is:  1
what unit is the measurement in? cm, m, or km: cm
you have chosen cm, converting to m and km
Traceback (most recent call last):
File "C:/Users/USER/Desktop/QUESTION.py", line 10, in <module>
m = num/100
TypeError: unsupported operand type(s) for /: 'str' and 'int'

1 个答案:

答案 0 :(得分:1)

我很惊讶它无处不在。使用global_step/sec函数,Python listed hereinput())时输入的内容。

基本上,您只需要在初始化str变量后使用num = int(num)将文本转换为整数。为了保持清洁,您还需要实现一个基本的错误处理系统,以防用户输入的输入无法隐式转换为整数。

例如:

num