Kenneth DeTalente
Kenneth DeTalente

Reputation: 35

Getting errors in simple code and can't find out why

New to programming and I'm trying to make my first program, but I'm getting errors and I don't know why. It's supposed to convert speed from miles per hour to kilometers an hour

unit = input(str("is your speed in mph or kmh? ")).lower()

value = float(input("what is your speed? "))

if unit == 'mph':
    result = str(unit * 1.6) + 'kmh'

if unit == 'kph':
    result = str(unit / 1.6) + 'mph'

else:
    pass

print(value, unit, "is equal to ", result)

Traceback (most recent call last): File "C:\Users\spenc\Desktop\Coding\speedconverter.py", line 5, in result = str(unit * 1.6) + 'kmh' TypeError: can't multiply sequence by non-int of type 'float'

Upvotes: 0

Views: 70

Answers (2)

Brandon Barney
Brandon Barney

Reputation: 2392

Oh that's an easy one. You're multiplying a 'str' object by a 'float'. Double check your code:

unit = input(str("is your speed in mph or kmh? ")).lower()

Returns a string.

You want to do this instead:

if unit == 'mph':
    result = str(value * 1.6) + 'kmh'
elif unit == 'kph':
    result = str(value / 1.6) + 'mph'
else:
    pass

You also likely meant this:

unit = str(input('Is your speed in mph or kmh?')).lower()

Since the former version was explicitly creating a string from a string. Also worth noting that by convention use ' ' for simple strings.

EDIT: To elaborate on string multiplication, lets take this example:

foo = 'foo'
foo * 3    # multiplication by int produces foofoofoo
foo * 2.5  # multiplication by double produces error

In the example above we know that multiplying the original string by an integer works. Why is this? Well it is as simple as essentially asking the program to make n foo's. This is easy to do. It would be like doing this:

'foo' + 'foo' + 'foo'

You'll note the use of the addition operator here (which is supported).

On the other hand, lets say we try this:

'a' * .5

What should the program do? We are asking for half of the letter a. The program could interpret this many ways (dividing the binary or hex representation of a by .5 for example) but there is no clear way to return an expected result. Instead, we just get an error.

What we could do is bypass the type error by explicitly converting our float to an int:

'a' * int(.5) # works and returns ''

Note here though that we get '' since int(.5) is 0 and this is where things get messy. Many would expect int(.5) to be 1 which can lead to unexpected behavior if you are not aware of the implicit behavior of the language.

This all leads to a very important point, and a reason why things like str * float lead to errors: the more implicit behavior we have (assuming you mean int(float) for example) can produce bugs (and in many languages it does) so instead Python is very explicit about what you do. If you want an int you have to ask for an int and this produces cleaner, easier to read (and understand), code.

Upvotes: 0

ViG
ViG

Reputation: 1868

You are using unit instead of value. Also note the difference in your if and your input. It is also recommened to use elif:

unit = input(str("is your speed in mph or kmh? ")).lower()
value = float(input("what is your speed? "))

if unit == 'mph':
    result = str(value * 1.6) + 'kmh'

elif unit == 'kmh':
    result = str(value / 1.6) + 'mph'
    print(result)

else:
    pass

print(value, unit, "is equal to ", result)

Upvotes: 2

Related Questions