1

So, I've been having a problem while trying to convert the variable t into a float, and it's giving me an error, saying "could not convert string to float: " with a space as 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()

I've been testing out with print commands inbetween and it prints the float value just alright, but in the if command it shows up an error, so it just prints "Wrong" every single time. When I remove:

if tl[-1]!='C' or tl[-1]!='K' or tl[-1]!='F' or t>200 or t<-100:
            print("Wrong")

The code works jus fine. Where is the problem?

I've been running the program on a private university platform, and this is what it returns:

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: 

When using Python it just prints "Wrong", without any error.

12
  • 3
    Post full error message with traceback please. Commented Nov 9, 2016 at 15:07
  • you'll never match any negative numbers with your method... Commented Nov 9, 2016 at 15:08
  • 2
    As an aside, why are you detecting t>200 as an error condition without considering units? 15°C ≈ 59°F ≈ 288 Kelvin. Commented Nov 9, 2016 at 15:09
  • 2
    The only way to get past that check would be for the final character to simultaneously be equal to 'C', 'K', and 'F'. Commented Nov 9, 2016 at 15:10
  • @jasonharper What? No. if c1 or (...) or cN: will evaluate to True if any of the conditions (c1 (...) cN) is True Commented Nov 9, 2016 at 15:13

1 Answer 1

1

Your problem isn't with converting to a float but with your condition.

write out the truth table and you'll find your mistake.

As a side note it's quite confusing using negation in a condition unless it's absolutely necessary.

try using the condition:

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

You also have a problem with your regex cutting out any minus signs so you will never get a negative temperature.

You should probably also check that the input actually contains some digits to convert to a float as trying to convert an empty string "" will return the error you are seeing.

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")

working with user input is always tricky since they can always type whatever they want in the box. using a try-except will handle most user error in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I hasn't been long since I started learning Python, and programming in general, so thank you vor pointing my mistake. I also changed the code a little bit to include negatives, I just used strings instead of lists. I still have to work a little bit on the code but that was my biggest worry.
No problem. Best of luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.