【发布时间】:2018-07-27 08:38:34
【问题描述】:
我用python写了一个长度转换器, 以下是代码:
active = True
while active:
option = input('please choose:\na:centimetre to inch \nb:inch to centimetre:')
if option == "a":
centimetre = input('Please enter centimetre:')
centimetre= float (centimetre)
inch = centimetre / 2.54
print(str(centimetre) + ' centimetre equals' + str(inch) + ' inches')
elif option == "b":
inch = input('Please enter inch:')
inch = float ( inch )
centimetre = inch * 2.54
print(str(inch) + ' inch equals ' + str(centimetre) + ' centimetre')
else:
print("sorry you entered wrong option。please enter 'a'or'b': ")
continue
status = input('continue? yes/no :')
if status == 'no':
active = False
这些代码在notepad++和 http://www.pythontutor.com/
但是当我尝试使用 pycharm 时,出现错误:
line 6, in <module>
centimetre= float (centimetre)
ValueError: could not convert string to float:
不确定问题出在哪里。有人遇到过这个问题吗?
【问题讨论】:
-
我猜他们被配置为使用不同的python版本。你用的是哪一个?
-
pi_float = float(pi_string)LINK -
在转换前尝试
print(centimetre)? -
@R.García 不是很有帮助,因为发布的代码本身是正确的
-
我在默认的 python3.6 IDLE 中尝试了这些代码它仍然有效
标签: python string input floating-point