0

Let's say i have a list of the alphabet

myList=["a","b","c"..."z"]    

Now lets say we have a variable within a loop that takes out a random letter from the list. Obviously random is imported.

while True:  
ans=myList[random.randint(1,26)]  

I want the user to be asked to take a guess at a letter so within the loop i add

guess=input('Take a guess at a letter from the alphabet')  

The user will receive a clue on the whereabouts of the answer

print('The letter locates between x and x.')

Question. How can i determine the position of ans in myList so i can give two random values and perhaps assign them to variables, one below ans and one value over ans.

The range would always be random between these two values so ans is not always the median of the two values.

p.s. I would put the script together to give a better view of what it looks like, but unfortunately i find the formatting help very confusing, and highlighting pieces of code and pressing Ctrl+K does not work as simply as i expected.

help-info.de
  • 6,695
  • 16
  • 39
  • 41
David
  • 65
  • 1
  • 5

2 Answers2

0

The position is the output of the random call, right?

You can save that to a variable before calling the myList[]

index = random.randint(1,26)
ans = myList[index]
Raquel Guimarães
  • 957
  • 1
  • 12
  • 18
0

use

myList.index(ans) 

for above code to work you need to have ans in myList or else it will throw an exception. BTW this question is similar to Finding the index of an item given a list containing it in Python

prudhvi Indana
  • 789
  • 7
  • 19