Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading