Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the question:
Write a shell (text-based) program, called fruit_list.py, that creates the list of fruit names ('Apple', 'Orange', 'Banana', 'Water Melon', 'Pear') and asks the user to pick a number from 1 to 5. The program then prints the corresponding item on the list. Note the difference between the item number and the list index. See the example below.

Example:
Pick a number between 1 and 5: 3
You got a Banana.
PS C:\Users\ssiva\Desktop>

What I have tried:

x = ['Apple', 'Orange', 'Banana', 'Water Melon', 'Pear']
y = [1, 2, 3, 4, 5]
for y in x:
print x[y]

while y > 5:
print "Pick a number between 1 and 5:"
int = raw_input()
Posted
Updated 23-Feb-18 11:00am

1 solution

x = ['Apple', 'Orange', 'Banana', 'Water Melon', 'Pear']
print("Pick a number between 1 and 5:")
while key > "5" or key < "1":
key = raw_input
print x[int(key)]
 
Share this answer
 
Comments
phil.o 23-Feb-18 17:06pm    
Please use <pre> tag to format your code.
This is especially important for python, since indentation has a meaning, logically speaking. For all other languages, indentation conveys no meaning by itself, but largely eases reading of the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900