I am trying to allow the user input to select specified options from a list

Question:

I have only been learning Python for about two weeks and need a bit of help with an assignment. Below is the code I have written so far,

import random 
colour_list = ["red","blue","white","yellow","pink","orange","black","green","grey","purple"]
start_num = int(input("enter a starting number between 0 and 4n"))
end_num = int(input("enter a end number between 5 and 9n"))
print ("You chose ",random.choice(colour_list))

The task is as follows "Ask the user for a starting number between 0 and 4 and an end number between 5 and 9. Display the list for those colours between that start and end numbers the user input"

Can anyone suggest how I would link the user input to the list? Thanks in advance

Asked By: dan93

||

Answers:

You don’t need to use random here. You can do simple list slicing,

colour_list = ["red","blue","white","yellow","pink","orange","black","green","grey","purple"]
start_num = int(input("enter a starting number between 0 and 4n"))
end_num = int(input("enter a end number between 5 and 9n"))

print("You chose ",colour_list[start_num:end_num])
Answered By: Rahul K P
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.