+ 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")
18 Antworten
+ 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])
+ 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.
+ 2
I’m not able clear task 4 as it is hidden and don’t know where I’m going wrong
+ 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
+ 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])
+ 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")
+ 1
Try this Out
if num < 0 or num > 7:
print("Wrong number")
else:
print(fruits[num])
+ 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])
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])
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
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")
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])
0
Have done this
if num >= 0 and num <=len(fruits):
print(fruits[num])
else :
print('Wrong number')
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')
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])
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])
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")
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")