+ 1

List V problem

Imagine a vending machine that sells fruits. Each fruit has its own number, starting from 0. Write a program for the vending machine, which will take n number as input from the customer and return the fruit with that index. Code: fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num >= (len(fruits) - (len(fruits) * 2)) and num <= len(fruits) - 1: print (fruits[num]) else: print ("Wrong number")

20th Oct 2020, 3:23 AM
Sagar Rana
Sagar Rana - avatar
18 Answers
+ 8
thats very complicated! try this! fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num <0 or num >7: print("Wrong number") if num >= 0 and num <= 7: print (fruits[num])
7th Apr 2021, 6:17 PM
Sirisha Tammina
Sirisha Tammina - avatar
+ 6
Try: if num < 0 or num >= len(fruits): print('Wrong number') else: print(fruits[num]) This is, of course, if you don't want negative indexes to work with the list, which are actually valid. Also, make sure your outputs are exactly what is expected given the result. I.E. if an invalid option is chosen, does the problem say to output 'Wrong numbers? Etc.
20th Oct 2020, 3:44 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
I’m not able clear task 4 as it is hidden and don’t know where I’m going wrong
20th Oct 2020, 3:25 AM
Sagar Rana
Sagar Rana - avatar
+ 1
Not sure, but maybe you can try this for checking number validity if num in range(len(fruits)): # print the fruit else: # wrong number
20th Oct 2020, 3:44 AM
Ipang
+ 1
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if (num <0 or num >7): print("Wrong number") else: print(fruits [num])
23rd Mar 2021, 10:54 PM
Ankit Dogra
Ankit Dogra - avatar
+ 1
Note this logic start from 1 not zero fruits = ["APPLE", "ORANGE", "GRAPE", "KIWI", "BANANNA", "PAZHAYAM", "CHAKA", "MATHALAM", "PAPAYA", "LOVELOLIKA", "MANGO"] l = len(fruits) num = int(input("Choose a number between 1 & "+str(l)+" for fruits : ")) if num > 0 and num <= l: print("Collect your fruit through outlet :-",fruits[num-1]) else: print("Invalid number")
20th May 2021, 6:26 AM
M Vishnu
M Vishnu - avatar
+ 1
Try this Out if num < 0 or num > 7: print("Wrong number") else: print(fruits[num])
5th Mar 2022, 10:21 PM
Denardo Riley
+ 1
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num < 0 or num >7: print("Wrong number") elif num == 0: print(fruits[0]) elif num == 1: print(fruits[2]) elif num == 2: print(fruits[3]) elif num == 3: print(fruits[4]) elif num == 5: print(fruits[5]) else: print(fruits[7])
17th May 2022, 3:01 AM
Najwan Najmussabah
0
@Sagar Rana, try this one as suggested by @ChaoticDawg: fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num < 0 or num >= len(fruits): print('Wrong number') else: print(fruits[num])
30th Jan 2021, 1:21 PM
Tsvetelin Anastasov
Tsvetelin Anastasov - avatar
0
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) # your code goes here items[num] = 'x' print(items) try this
4th Jul 2021, 10:21 PM
Hugues Rodrigue Tha Andela
Hugues Rodrigue Tha Andela - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num in range(0,8): print(fruits[num]) else: print("Wrong number")
30th Nov 2021, 8:53 AM
Dr_Chipo
Dr_Chipo - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num < 0 or num > 7: print("Wrong number") else: print(fruits[num])
2nd Dec 2021, 9:16 AM
Noshin Anwar
Noshin Anwar - avatar
0
Have done this if num >= 0 and num <=len(fruits): print(fruits[num]) else : print('Wrong number')
16th Dec 2021, 3:13 PM
Marcelo Goes de Freitas
Marcelo Goes de Freitas - avatar
0
l=['apple','banana','cherry','kiwi','mango','litchi','grapes','ice apple','oranges'] print('The list of fruits is: ',l) n=int(input('Enter the number you want: ')) if(n<len(l)): print('The fruit corresponding to that number is : ',l[n]) else: print('No fruits corresponding to that number')
21st Dec 2021, 5:15 PM
SANVIKA
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num < 0 or num >7: print("Wrong number") elif num == 0: print(fruits[0]) elif num == 1: print(fruits[2]) elif num == 2: print(fruits[3]) elif num == 3: print(fruits[4]) elif num == 5: print(fruits[5]) else: print(fruits[7])
8th Jan 2022, 7:10 PM
Ihor Princ
Ihor Princ - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num < 0 or num >= len(fruits): print('Wrong number') else: print(fruits[num])
15th Nov 2022, 11:33 AM
Pooja Patel
Pooja Patel - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num == 0: print(fruits[0]) elif num == 1: print(fruits[1]) elif num == 2: print(fruits[2]) elif num == 3: print(fruits[3]) elif num == 4: print(fruits[4]) elif num == 5: print(fruits[5]) elif num == 6: print(fruits[6]) elif num == 7: print(fruits[7]) else: print("Wrong number")
27th Jan 2023, 4:38 AM
Himasha Nethmini
Himasha Nethmini - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num >= (len(fruits) - (len(fruits) * 2)) and num <= len(fruits) - 1: print (fruits[num]) else: print ("Wrong number")
17th Feb 2024, 4:18 PM
Sushanta Dey
Sushanta Dey - avatar