- 1
Trying to solve food vending machine in python core
My code Only three test case come out right fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) if num <0 or num>7: print("Wrong number") elif num >=0 and num <=7: print(fruits[5])
8 Respostas
+ 1
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"]
num = int(input())
#your code goes here
if num <= 7 and num >=0:
print(fruits[num])
elif num < 0 or num >7 :
print("Wrong number")
0
do print(fruits[num]) instead of print(fruits[5])
as, putting 5 in will only print the 5th array(i.e -: pear), not the required fruit you wished for.
0
Hi. It seems that when the input number is between 0 and 7 (including them), the code print the "fruits[5]" value, which is "pear". But this don't cover all of the list possibilities.
For this, you can use the input number "num" as the fruit list index, just like this: fruits[num]. So the final line becomes "print(fruits[num])". The code may work now.
0
Thanks
0
but actually it does'nt works it says "n" is not defined
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"]
num = int(input())
#your code goes here
if(num>=0 and num<=7):
print(fruits[num])
elif(num<0 or num>7):
print("Wrong number")
- 1
If n< 0 or n>7 (the index of last fruit ), the program outputs "Wrong number".
Sample Input:
2
Sample Output:
banana
- 3
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.