- 1

Fruit Vending machine challenge

I’ve been stumped by that problem. Can someone help so I could figure out how to use the list function that way. Im referring to the fruit vending machine challenge in python where user inputs number and a fruit name is shown. 7 fruits.

29th May 2021, 12:53 AM
Robert Martinez
Robert Martinez - avatar
14 Réponses
+ 2
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]]) elif 0> num > 7: print("Wrong number")
29th May 2021, 1:47 AM
Robert Martinez
Robert Martinez - avatar
+ 1
Please show me your code first. I can help with that, but I need to understand your code first.
29th May 2021, 1:17 AM
WolfRanger456
WolfRanger456 - avatar
+ 1
0 > num > 7 is shortcut for: 0 > num and num > 7 and is not possible, as 0 > 7 is false... you should separate it in two condition linked by a OR conditional operator: 0 > num or num > 7 however, you doesn't need all this verbose if..elif stuff: as said by David Ashton "you need to print fruits[num]"... by using if..else: if 0 > num or num > 7: print("Wrong number") else: print(fruits[num]) ... or simply: if 0 <= num <= 7: print(fruits[num]) else: print("Wrong number")
29th May 2021, 4:28 AM
visph
visph - avatar
+ 1
You can even boil it down more: print(fruits[num] if num < 8 else "Wrong number") Assuming negative numbers will not be input.
29th May 2021, 5:31 AM
David Ashton
David Ashton - avatar
+ 1
This works fine and captures negative numbers: fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if num < 0: print('Wrong number') elif num <= 7: print(fruits[num]) elif num > 7: print('Wrong number')
17th Jul 2021, 5:06 PM
James
James - avatar
0
fruits = ["apple", "cherry", "banana", "kiwi", "lemon", "pear", "peach", "avocado"] num = int(input()) #your code goes here if 0 <= num <= 7: print([0,1,2,3,4,5,6,7]) else: print("Wrong number")
29th May 2021, 1:19 AM
Robert Martinez
Robert Martinez - avatar
0
Your first print statement just prints a list of the index positions of the 7 fruit. If you want to print out the fruit at index position num, you need to print fruits[num].
29th May 2021, 1:30 AM
David Ashton
David Ashton - avatar
0
I did this one and the program says invalid syntax on line 4
29th May 2021, 1:48 AM
Robert Martinez
Robert Martinez - avatar
0
Ok I changed them all the = to == but there is still an error with the last line which is the equality statement.
29th May 2021, 2:03 AM
Robert Martinez
Robert Martinez - avatar
0
Wow thanks. I knew there had to be a short concise way to code this. I was able to do the other coding challanges concisely. I guess I had a difficult time umderstanding how to use this function.
2nd Jun 2021, 3:54 PM
Robert Martinez
Robert Martinez - avatar
0
import java.util.Scanner; class HelloWorld { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String[] menu={"Tea","Espresso","Americano","Water","Hot chocolate"}; int choice = sc.nextInt(); switch(choice) { case 0: System.out.println(menu[0]); break; case 1: System.out.println(menu[1]); break; case 2: System.out.println(menu[2]); break; case 3: System.out.println(menu[3]); break; case 4: System.out.println(menu[4]); break; default:System.out.println("Invalid"); } } }
26th Sep 2023, 9:31 AM
Amrit Kumar Ojha
Amrit  Kumar Ojha - avatar
0
What is the error here
26th Sep 2023, 9:32 AM
Amrit Kumar Ojha
Amrit  Kumar Ojha - avatar
0
Please help me
26th Sep 2023, 9:32 AM
Amrit Kumar Ojha
Amrit  Kumar Ojha - avatar
- 1
I'm using this code and getting an error even though my "expected output" (Wrong Number), matches the Actual output from my code... if num >= 0 and num <= 7: print(fruits[num]) else: print("Wrong Number") I then changed it to this: if num >= 0 and num <= 7: print(fruits[num]) elif num < 0 or num > 7: print("Wrong Number") It still correctly gives "Wrong Number" with an input of 12, still incorrect according to Sololearn
22nd Dec 2021, 8:22 PM
Mahri Bahati