I am working on a conversion table website as my final project. I am using a base unit multiplier to convert one unit to the other. So for example centimeter to yard would look like this.

user_input = amount to be converted

first base unit multiplier = whatever unit chosen to convert from the base unit multiplier, for example it's metres for length

second base unit multiplier = whatever unit chosen to convert to the base unit multiplier

user_input * (base unit multiplier cm to meters) * (base unit multiplier meters to yards)⁻¹

I am running into a snag when I try to multiply the two multipliers with the user_input. I have to change my type list to an int and I have tried three ways. First with row_1 = map(int, row_1), second is number_1 = [ int(x) for x in row_1 ], and third row_1[0]["BUM"]. Neither seem to be working as I have gotten TypeErrors now twice. Is there some very straight forward technique I am overlooking here to make this work? I could add more code if you needed but the final line in the below is where I am getting the error.

Edit #2

I thought the error was with multiplying but it has to do with the parameter substitution. I have use '%s' and a '?' and keep getting the TypeError.


I have tried to simply input a string, for example (Metre,), ("Metre",), (Metre), and ("Metre") and I am still getting the same error. I have added pictures of my SQL table because maybe something is wrong with it?

The structure of my table

The contents of my table

    amount = request.form.get("amount")

    if not amount:
        return apology("Enter some numbers")
    elif amount.isalpha():
        return apology("No letters please")
    elif any(c.isalpha() for c in amount):
        return apology("I said no letters!")

    row_1 = db.execute("SELECT bum FROM conversion WHERE unit = ?", (convert_from,))
    row_2 = db.execute("SELECT bum FROM conversion WHERE unit = ?", (convert_to,))

    conversion_result = int(amount) * row_1[0]["bum"] * row_2[0]["bum"]**-1

    return render_template("length_converted.html", result = conversion_result 

Turns out I had to us the semicolon. The below works.

row_1 = db.execute("SELECT bum FROM conversion WHERE unit = :id", id = 
row_2 = db.execute("SELECT bum FROM conversion WHERE unit = :id", id = 
| improve this answer | |
  • Great. Now, can you please accept one of the answers? Otherwise, it'll sit in the unanswered question pool forever. ;-) – Cliff B Dec 23 '17 at 6:06

Not understanding how your maths relate to your code, but shouldn't it be id=convert_from and id=convert_to?

And I'd expect something like conversion_result = float(amount) * row_1[0]["BUM"] * row_2[0]["BUM"]

For some reason, I'd also expect one of those * to be a /. For example, if converting from metres to feet, one would multiply by 1, or 3.28084ft/1m.

| improve this answer | |
  • I have edited my question to clarify the formula. I should've also put that I tried row_1[0]["BUM"] and I get the error 'list index out of range'. Changing it to id=convert_from did not work. – SozDaneron Dec 14 '17 at 14:20
  • Is it possible that row_1 or row_2 is None because your SQL statement did not match anything? ["convert_from"] definitely doesn't look right. – Blauelf Dec 14 '17 at 18:48
  • I think that must be it. After reading up on lists, it must be a problem with my SQl code which I was just looking at now, thanks. – SozDaneron Dec 14 '17 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .