Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Troubleshooting  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.
Could someone tell me why isn't my code working?



So the program is like a conceptual fitness app. where the user types in the food he ate and its kcal and separates it with a ",". The kcal of the eaten food is then taken from the limit. Everything is stored in a list (for statistics or whatever). This goes on until the user says "thatsit". Then the it breaks out of the while loop and prints out the list and the remaining kcal for the day (this is shown, after every input).
Everything goes fine until I want to finish the input.

   limit = limit - int(food[1])
IndexError: list index out of range

Also, would it be smarter to store the eaten food in touples instead of lists in a list.
 
Sheriff
Posts: 24291
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's that "član" thing in line 6?
 
Marshal
Posts: 63823
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wiktionary says it means “article”.
 
M. Gumblert
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, I was translating the code for english speakers. It's the "food"
 
Campbell Ritchie
Marshal
Posts: 63823
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful about translating code; it is usually better to post it exactly because you may introduce or remove errors in translation. It is useful, however, to have English translations of the variable names; thank you for that thoughtfulness.
 
Greenhorn
Posts: 26
Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi M. Gumblert,

Is the code shown extracted from a larger program?  Did you mean to have lines 8 and 9 indented at the same level as line 7?  Your description of what the code is supposed to do seems to indicate otherwise.  I also appreciate your wanting to translate the code for those of us who are language handicapped. Perhaps it would be helpful to temporarily try running the translated code to be sure it fails in the same way?

Thanks,
Travis
 
M. Gumblert
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear everyone,

thank you for your help. As most of you suggested I translated the code. Ignore the one I started the topic with. Here is the whole code. The idea is a really basic program that counts how much kcal you can eat a day. When you type in your meal in the format: "salad, 300" the program adds that to a list, and takes 300 from your daily kcal that is defined based on your weight. All the numbers are arbitrary. The problem remains the same:

kcal_limit = kcal_limit - int(meal[1])
IndexError: list index out of range

Here is the code:
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi M. Gumblert,

Let's start from the beginning.

What would happen if one would enter weight of 110 kg? (pretty much mine).
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

M. Gumblert wrote:kcal_limit = kcal_limit - int(meal[1])
IndexError: list index out of range


Please tell, what input you have entered as for meal?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, why do you add those extra parentheses eaten.split((","))? Maybe that is part of an issue. After you split string, try to print its length on the following line and see what you get. Also try to print the content of meal array, and see whether it is something what you expect.
 
Travis Risner
Greenhorn
Posts: 26
Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, M. Gumblert,

Thanks for including the whole program, and for translating it to English.  That helps to focus the actual issue without other distractions.  Thanks also for including the comment in line 29 so we understand why the following two print statements have been included.

To understand why this code is failing, we need to understand the timing of when each statement is executed relative to the other statements in the loop.  Although the test for stopping the loop (at line 24) is physically before the rest of the code in the loop, the value received in the variable eaten is not tested until after all the code in the loop (ending with line 31) has run and the loop starts over.  Thus the split (at line 26 assumes that the answer in eaten has a comma, and  the code at line 28 assumes that meal now contains two values in the list.  

Hopefully this is enough of a nudge to work out a successful solution!

Travis
 
M. Gumblert
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Travis Risner
Thank you for your long answer I will try to rewrite the code.
@Liutauras Vilda
Thank you for your answers and pointing out the problems with split(","). I know that there are some logical problems (like the 110kg thing). I didn't bother with that because those are some problems I will look into later.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!