1

I am quite confused on this, so I hope someone can help me.

So what I have is basically a list in Python, then I have set a variable which within I have used the list name and called the first element such as myVariable = myList[0], now basically what I want to do is to add an integer value to myVariable to make it so that I get the next value in the list myList[1] and subtract the list as a whole by one each time. I have 8 different elements/items in the list so far.

I keep getting an error usually something along the lines that I can't add an integer (the number value) to the myVariable as it sees it as a string, and I'm unsure on how to get the list to move one result forward and once that's done then subtract the list by one result as it is intended to be removed each time it loops.

Example:

def testFunction(testValue):
    myVariable = myList[0]
    testResult (myVariable + testValue) - 1
    print testResult

The testValue being an integer, I need to get the value from myList depending on the integer value of testValue (which it's being added with), so if the testValue was like 3 it would add that to the default myVariable value which is currently 0, then after that result from the list and continue doing this until there is only one result left in this list, which currently consists of 8 elements.

UPDATE:

Sample list: ["KaPow", "Boom", "Pow", "Shapow", "Crash", "Whoosh", "Bang", "Pew"] I enter a value into a function such as 3, function below: bam(bamtypes, choice): I enter the suggested value of 3 as "choice" and the bamtypes is just the given list above, then I should receive back the value Shapow. Then what I want to do is basically add myVariable to testValue (I referred to the integer of "choice" in the function when referring to testValue, so in this case it'll be 3). I also referred to bamtypes when saying myList. As myVariable basically set to tell the function to begin from the first element in the list such as myVariable = bamtypes[0], but then I want the 3 value from "choices" to be added to the myVariable, so that it becomes bamtypes[3] then I can use .pop to extract that value, then does it again so it is bamtypes[6] and so on until the list only leaves one result. Also note as the list has 8 elements, what I want to do is once the 3 adds to the bamtypes[6] it should give me the value of [0] again as it resets, counting twice to the 8th element then returns once more to 0, using up the value 3 for that given retrieval.

3
  • 3
    Sometimes, sample input and expected output help better than lengthy writeups. Commented Nov 16, 2013 at 0:59
  • @Upvoter, Would you mind explaining the question a little bit? Commented Nov 16, 2013 at 1:43
  • It sounds like you want to iterate through the items in a list. You should use a loop in order to do this. Commented Nov 16, 2013 at 2:37

1 Answer 1

2
  1. When you are doing an arithmetic operation, you can either store the result in a variable or ignore the result. If we want to store in a variable, we need to use assignment operator, like this

    testResult = (myVariable + testValue) - 1    # Note the `=` symbol
    
  2. The error you get means that the data stored in myVariable is not a number but a character string. So, you should convert that to an integer like this

    testResult = (int(myVariable) + testValue) - 1    # Note the `int` function
    
Sign up to request clarification or add additional context in comments.

5 Comments

The error with = was a mistake, oops sorry! Also I've tried the second point you've suggested with converting it to an int and I get this error -- ValueError: invalid literal for int() with base 10: "Shapow"
@Shaz It means that the list has string values not numbers. How can we add numbers to strings? :) Could you please update your question with sample input and expected output?
Alright I have updated my question, I hope it's a bit more insightful for you. I've tried to cover as much as possible with what I want to actually do. Sorry for the trouble.
@Shaz Do you want return and delete the items based on the choice?
I want to return the next value based on the choice but subtract -1 each time I choose the next value based on choice. If that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.