Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Why am I getting a "can't multiply sequence by non-int of type 'float'" error even when x is declared as a number in this function?

$
0
0

I am new to python and assigned with creating a simple function that converts temperature of one kind (C, F, K) to another. Simple enough, but when using x, default value set to 0, as an input in my function I am getting a "can't multiply sequence by non-int of type 'float'" error. I cant seem to figure out what I am doing wrong. my code and then the error is attached below.

import warningsdef convTemp(x=0, fro="C", to="F"):if fro == "C" and to == "F":    x = 1.8 * x + 32    return xelif fro == "C" and to == "K":    x = x + 273.15    return xelif fro == "F" and to == "C":    x = x * (5/9)-32    return tempelif fro == "F" and to == "K":    x = (x * (5/9)-32)+273.15    return xelif fro == "K" and to == "C":    x = x - 273.15    return xelif fro == "K" and to == "F":    x = (9/5)*(x - 273.15) + 32    return xelse:    if fro == to:        warnings.warn("Your 'fro' parameter is the same as your 'to' parameter!")TypeError                                 Traceback (most recent call last)<ipython-input-2-e9e7ea7efd49> in <module>      1 assert convTemp(0) == 32----> 2 assert convTemp([0,10,20]) == [32, 50, 68]      3 <ipython-input-1-a8e50b49bfb6> in convTemp(x, fro, to)      2 def convTemp(x=0, fro="C", to="F"):      3     if fro == "C" and to == "F":----> 4         x = 1.8 * x + 32      5         return x      6     elif fro == "C" and to == "K":TypeError: can't multiply sequence by non-int of type 'float'

I have attempted to fix this by declaring x to have a number value before the function and I have also tried explicitly listing x as an int, a float, and a list.


Viewing all articles
Browse latest Browse all 23131

Trending Articles