vendredi 4 août 2023

Argument of type "list[str]" cannot be assigned to parameter "__value" of type "str" in function "index". How do I fix this?

I have been programming a type based combat game - think of it a bit like Pokemon. In order to win a round and hit the opponent, the computer roles a virtual 6 sided dice for each player and the highest roll wins.

There are different advantages you can obtain, one being a "wizard" that increases your chances on rolling a higher number.

As I'm relatively new to python, the use of "random.choices" and the weight argument was the only way I could think to achieve the higher probability rolls. However, this has led to the issue where, when I try to return the value, it produces the error that the value has to be a string or number not in a list:

def wizardSpeed():
    dice_num = [1, 2, 3, 4, 5, 6]
    probability = [0.25, 0.45, 0.65, 0.85, 0.9, 0.85]
    dice_roll = random.choices(dice_num, probability)
    return dice_roll

I tried to fix this (with my limited knowledge :/) by obtaining the position of the value in the list, then converting the one value in that position to an integer before returning it. I then got a similar error (one in title):

def wizardSpeed():
    dice_num = ["1","2", "3", "4", "5", "6"]
    probability = [0.25, 0.45, 0.65, 0.85, 0.9, 0.85]
    dice_roll = random.choices(dice_num, probability)
    position = dice_num.index(dice_roll)
    result = int(dice_num[position])
    return result

Is there a way to fix this? I am trying to take one value from the list to be used as an integer in a variable later in the program.




Aucun commentaire:

Enregistrer un commentaire