![]() |
problem with ranges and lists - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: problem with ranges and lists (/Thread-problem-with-ranges-and-lists) |
problem with ranges and lists - J4W3S - 03-22-2013 Hey guys, i started to code a text base game on python but ran in to a problem. I'm using a list to save different words for directions so the user can type things like east or left to go left. The line of code that is giving me trouble is. if direction ==left[range(8)]: This is the error i get, TypeError: list indices must be integers, not range Any idea how to fix this? Here is the rest of the code as well in case that helps. Code: left = 'left LEFT Left east EAST East notright port'.split() RE: problem with ranges and lists - Ex094 - 03-22-2013 EDIT: Ok so your problem is that in this piece of code: Code: if direction ==left[range(8)]: So you should try a for loop here as I did: Code: left = 'left LEFT Left east EAST East notright port'.split() Explanation: Code: for i in range(len(left)): Tell me if it solves your problem. RE: problem with ranges and lists - J4W3S - 03-22-2013 hey, thanks didn't think of that. but i got it to work anyway there's this cool in function i didn't know about it goes like if direction in left: thanks for helping though i'm just learning so it's good to know different ways of doing things. Also just tried you code and it works as well. in case you were wondering. RE: problem with ranges and lists - ArkPhaze - 03-22-2013 You know you don't even have to loop through the values.... Code: if direction in left: Using a loop for this in Python is a waste of time. :whistle: RE: problem with ranges and lists - 1llusion - 03-23-2013 (03-22-2013, 05:17 AM)Ex094 Wrote: left = 'left LEFT Left east EAST East notright port'.split() I'm no Python coder, but I think this will go with every language: What if the user typed: lEfT or LefT? Your code might not do what it should then. So instead of declaring words with capital letters on different places, simply set the input to lower or to upper. I'd imagine it would work something like: (PS: Not actual Python code, just example) Code: left = 'left east notright port'.split() This way you save some space and make the valid input range much bigger ![]() RE: problem with ranges and lists - 1llusion - 03-23-2013 (03-22-2013, 05:17 AM)Ex094 Wrote: left = 'left LEFT Left east EAST East notright port'.split() I'm no Python coder, but I think this will go with every language: What if the user typed: lEfT or LefT? Your code might not do what it should then. So instead of declaring words with capital letters on different places, simply set the input to lower or to upper. I'd imagine it would work something like: (PS: Not actual Python code, just example) Code: left = 'left east notright port'.split() This way you save some space and make the valid input range much bigger ![]() RE: problem with ranges and lists - ArkPhaze - 03-24-2013 True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Question-problem-with-ranges-and-lists?pid=126843#pid126843 Absolutely NO need for a loop. RE: problem with ranges and lists - ArkPhaze - 03-24-2013 True, combine that logic with my method above: http://www.hackcommunity.com/Thread-Question-problem-with-ranges-and-lists?pid=126843#pid126843 Absolutely NO need for a loop. RE: problem with ranges and lists - J4W3S - 03-27-2013 thanks for the advice, especially the direction.toLower part. and sorry for the late reply i have been without power since Friday because of a snow storm. |