Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

python - ValueError: not in the list

I am a beginner in Python and I am having some trouble getting the index from a list.

Here is my code:

color=['red','green' ,'yellow' , 'blue', 'grey']
gg=random.sample(color, 1)
print(gg)
jj=(farba.index(gg))

What I'm getting:

['green']
ValueError: ['green'] is not in list

Can anybody help me?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

gg is a list, not a string. The list ['green'] is not in color, the string 'green' is. Try using random.choice() instead of sample().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...