简体   繁体   中英

Using arithmetic operators with a variable holding a list

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.

  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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM