- 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.
14 Answers
+ 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")
+ 1
Please show me your code first. I can help with that, but I need to understand your code first.
+ 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")
+ 1
You can even boil it down more:
print(fruits[num] if num < 8 else "Wrong number")
Assuming negative numbers will not be input.
+ 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')
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")
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].
0
I did this one and the program says invalid syntax on line 4
0
Ok I changed them all the = to == but there is still an error with the last line which is the equality statement.
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.
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");
}
}
}
0
What is the error here
0
Please help me
- 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