- 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])

2nd Apr 2021, 3:39 PM
Simisola Osinowo
Simisola Osinowo - avatar
8 Respuestas
+ 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")
15th Aug 2021, 10:33 PM
Moayad Sr
Moayad Sr - avatar
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.
2nd Apr 2021, 3:56 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
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.
2nd Apr 2021, 3:57 PM
thequietprogrammer
thequietprogrammer - avatar
0
Thanks
2nd Apr 2021, 3:59 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
but actually it does'nt works it says "n" is not defined
11th May 2021, 9:07 AM
Abigith Boovaragavan
Abigith Boovaragavan - avatar
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")
9th Jun 2022, 3:02 AM
Aasritha Sai Vemulapalli
- 1
If n< 0 or n>7 (the index of last fruit ), the program outputs "Wrong number". Sample Input: 2 Sample Output: banana
2nd Apr 2021, 3:40 PM
Simisola Osinowo
Simisola Osinowo - avatar
- 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.
2nd Apr 2021, 3:40 PM
Simisola Osinowo
Simisola Osinowo - avatar