Could not convert string to float


#1

please find the piece of code

def compute_bill(food):
    total = 0.0
    for each in food:
        total += float(each)
    return total

I am getting correct answer but the debugger is throwing an error " could not convert string to float"

please help in resolving this error to move forward


#2

exactly which exercise is this? Please provide the url


#3

https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


#4

food is the shopping_list you defined on line 1, so each will hold the product name (apple, banana and so on), you can not cast a string to a float.

You need to use each to access the prices dictionary, and there is no need to use float


#5

thank you ! understood the logic, I got the answer!!


#6

Now i am trying the Scrabble score program
i am getting the below error
Oops, try again. Remember to make your function work for upper- and lowercase letters!
my code is available at

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096

could you help me to resolve


#7

Can you please paste your code? :slight_smile:


#8

You might didn't make your code case independent. You may try this, it works for me :

def scrabble_score(word):
point=0
#words=word.lower()
for letter in word.lower():
point=point+score[letter]
return point
print scrabble_score('Welcome')


#9

core = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score(word):
if word.isalpha() and len(word) >0:
word.lower()
total_score = 0
for ch in word:
total_score += score[ch]
return total_score
else:
print "Input word incorrect"


#10

Little correction in your code :
core = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score(word):
if word.isalpha() and len(word) >0:
word=word.lower()
total_score = 0
for ch in word:
totalscore +=****core[ch]_
return total_score
else:
print "Input word incorrect"
print scrabble_score('Welcome')


#11

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score(word):
if word.isalpha() and len(word) >0:
word.lower()
total_score = 0
for ch in word:
total_score += score[ch]
return total_score
else:
print "Input word incorrect"

i am sorry
S letter in score not copied
please consider this code


#12

it worked
after changing word = word.lower()


#13