Home > Software engineering >  Make the string into the variables name
Make the string into the variables name

Time:12-16

i want to create a nontransitiv dice game

A = [3, 5, 7]
B = [2, 4, 9]
C = [1, 6, 8]

choice1 = input("Chose a Dice (A/B/C)")
result1 = random.choice(choice1)

The user will enter a string like "A" and i want it to become the variable A so that the random.choice function will output a random number from the corresponding list

CodePudding user response:

You could create a dictionary with the letters as keys and the arrays as values. Then, whatever the user puts, key into the dictionary.

diceDict = {
  "A" = [3, 5, 7],
  "B" = [2, 4, 9],
  "C" = [1, 6, 8]
}

result = random.choice(diceDict[choice1])

CodePudding user response:

I don't know what language this is written in but you need to perform some sort of check on the user entered value.... e.g

(pseudocode)

if (choice1 === "A") getRandomFromArray(A) 
if (choice1 === "B") getRandomFromArray(B) 
.... etc...
  • Related