Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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 ValueError:无法将字符串转换为浮点:_Python - Fatal编程技术网

Python ValueError:无法将字符串转换为浮点:

Python ValueError:无法将字符串转换为浮点:,python,Python,所以,我在尝试将变量t转换为浮点时遇到了一个问题,它给了我一个错误,说“不能将字符串转换为浮点:”空格为false def FtoC(a): a=(t-32)*1.8 return a def FtoK(a): a=(a-32)*1.8+273 return ta def CtoF(a): a=a*1.8+32 return a def CtoK(a): a=a+273 return t def KtoC(a): a=a-2

所以,我在尝试将变量t转换为浮点时遇到了一个问题,它给了我一个错误,说“不能将字符串转换为浮点:”空格为false

def FtoC(a):
    a=(t-32)*1.8
    return a
def FtoK(a):
    a=(a-32)*1.8+273
    return ta
def CtoF(a):
    a=a*1.8+32
    return a
def CtoK(a):
    a=a+273
    return t
def KtoC(a):
    a=a-273
    return a
def KtoF(a):
    a=(a+459.67)*5/9
    return a
########################################################
import re
t=input()
while True:
    if t=='end':
          break
    tl=list(t)
    t=re.findall('\d+', t)
    t=''.join(t)
    t=float(t)
    if tl[-1]!='C' or tl[-1]!='K' or tl[-1]!='F' or t>200 or t<-100:
        print("Wrong")
    else:
        if tl[-1]=='C':
            print("%.2f" % t,'C',CtoF(t),'F',CtoK(t),'K')
        if tl[-1]=='K':
            print("%.2f" % t,'K',KtoC(t),'C',KtoF(t),'F')
        if tl[-1]=='F':
            print("%.2f" % t,'F',FtoC(t),'C',FtoK(t),'K')
    t=input()

使用Python时,它只打印“错误”,没有任何错误。

您的问题不是转换为浮点,而是您的条件

写出真相表,你就会发现你的错误

作为旁注,除非绝对必要,否则在条件中使用否定是相当混乱的

尝试使用以下条件:

allowed_scales = ["C", "K", "F"]
if tl[-1] in allowed_scales and -100<t<200:
    # do your work here
else:
    print("wrong")

使用用户输入总是很棘手的,因为他们总是可以在框中键入他们想要的任何内容。在这种情况下,使用try-except将处理大多数用户错误。

请发布带有回溯功能的完整错误消息。您的方法永远不会匹配任何负数…顺便说一句,为什么您在不考虑单位的情况下将
t>200
检测为错误条件?15摄氏度≈ 59华氏度≈ 288开尔文。通过该检查的唯一方法是最后一个字符同时等于“C”、“K”和“F”。@jasonharper什么?否。
如果c1或(…)或cN:
将计算为
如果任何条件(c1(…)cN)为
非常感谢。我开始学习Python和编程的时间不长,所以谢谢你指出我的错误。我还对代码做了一些修改,加入了否定词,我只是使用了字符串而不是列表。我仍然需要在代码上做一些工作,但这是我最大的担忧。没问题。祝你好运。
Non-zero exitcode 1
*** Program stderr output following ***
Traceback (most recent call last):
  File "my_code.py3", line 28, in 
    t=float(t)
ValueError: could not convert string to float: 
allowed_scales = ["C", "K", "F"]
if tl[-1] in allowed_scales and -100<t<200:
    # do your work here
else:
    print("wrong")
while True:
    # in python 2:
    t = raw_input()
    # in python 3:
    t = input()
    if t=='end':
          break
    try:
        t=float(t[:-1]) # -1 to cut off the scale type
    except ValueError:
        print("Not valid input. try again.")

    allowed_scales = ["C", "K", "F"]
    if tl[-1] in allowed_scales and -100<t<200:
        if tl[-1]=='C':
            print("%.2f" % t,'C',CtoF(t),'F',CtoK(t),'K')
        if tl[-1]=='K':
            print("%.2f" % t,'K',KtoC(t),'C',KtoF(t),'F')
        if tl[-1]=='F':
            print("%.2f" % t,'F',FtoC(t),'C',FtoK(t),'K')
    else:
        print("wrong")