-2

I crawled the web but no answer (specific to solution). I got stuck with the following:

coin = [2.0 , 1.0 , 0.5, 0.1, 0.05, 0.02, 0.01]
kum=0.0

for i in coin :
while True :
    if kum + coin[i]  >= x :
# Intial (first loop) this should be equal to: if 0 + 2.0 > users input
# Second loop should be equal to if 2.0 + 1.0 > users input
        break
    else :
        kum +=  coin[i]

Console says:

Traceback (most recent call last):
  , line 13, in <module>
if kum + coin[i]  >= x :
TypeError: list indices must be integers, not float

I was wondering what I'm doing wrong. The list item is float? Adding 0.0 and float should not be the problem? Any help is appreciated. `

Josi Dunk
  • 51
  • 4

3 Answers3

2

Your code is broken, and the compiler has told you what the problem is.

You are trying to index a list with a float. The index must be an integer, but this:

for i in coin :
while True :
    if kum + coin[i]  >= x :
        break
    else :
        kum +=  coin[i]

Is using the values from each element in coin to index coin itself, so the indexes will be [2.0 , 1.0 , 0.5, 0.1, 0.05, 0.02, 0.01]

So what you are asking is for the second, first elements, and then element 0.5 etc..

You probably mean

if kum+i >=x :

Additionally, you have an infinite loop inside your for. Remove the while True : as follows:

for i in coin :
    if kum+i >=x :
        break
    else :
        kum += i

Then, the loop will execute once for each element in coin, and i will be set to the values in coin[], ie:

  • Iteration 1, i=2.0
  • Iteration 2, i=1.0
  • Iteration 3, i=0.5

etc

mjs
  • 2,837
  • 4
  • 28
  • 48
  • sorry, i forgot to explain what i wanna do: if kum + coin[i] >= x -> this should add the value of coin[i] and kum and compare it with x. So initial kum should be 0 and coin [0] = 2.0 ( first entry in array coin). this shall be done with each element of the coin. So this line is throwing the error. – Josi Dunk Nov 04 '16 at 12:13
  • If you remove the while, then the loop will execute once for each value of i, and i will contain the values from coin. Let me update. – mjs Nov 04 '16 at 12:21
1

You are passing a float value as the index for the coin list.

Change your for statement to say:

for i in range(0, len(coin)):

Then you will be passing the integer index (instead of the value of the item in the list)

Alternatively, you could just take i as the value and just use that:

for i in coin:
    while True:
        if kum + i >= x:
Brynn McCullagh
  • 4,083
  • 2
  • 17
  • 12
0

Thanks guys, the problem was :

for i in coin :

changed that to:

for i in range(0, len(coin)): 

Afterwards this makes a lot of sense to me... just for the record: This "masterpiece" :) of code should check how much coins you need reach a specific value. user input is the value, coin the array for the coins exisiting (EUR).

x = input("Bitte Euro Betrag eingeben: ")
coin = [2.0 , 1.0 , 0.5, 0.1, 0.05, 0.02, 0.01]
coinscount = [0 , 0 , 0 , 0 , 0 , 0 , 0]
kum=0.0

if min(coin) > x :
print("Betrag zu klein")
exit 
for i in range(0, len(coin)):
while True :
    if kum + coin[i]  > x :
        break
    else :
        kum = kum +  coin[i]
        coinscount[i] += 1


print("Anzahl Muenzen ist: ", sum(coinscount))

for i in range(0, len(coin)):
print("Sie benoetigen: ", coinscount[i], "Stueck", coin[i])

Thanks a lot, Josi

Josi Dunk
  • 51
  • 4