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

Time:10-22

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 4\n"))
end_num = int(input("enter a end number between 5 and 9\n"))
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

CodePudding user response:

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 4\n"))
end_num = int(input("enter a end number between 5 and 9\n"))

print("You chose ",colour_list[start_num:end_num])
  • Related