Fruit bowl | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Fruit bowl

I’m using python to solve a exercise. You have a bowl on your counter with an even number of pieces of fruit in it. Half of them are bananas, and the other half are apples. You need 3 apples to make a pie. Task Your task is to evaluate the total number of pies that you can make with the apples that are in your bowl given to total amount of fruit in the bowl. Here is my code for the Fruit bowl exercise: y=int(input()) y//=2 print(y//3) I got error for output. What’s wrong with my code?

31st Dec 2020, 6:13 AM
Haha36
23 Answers
+ 16
y = int(input()) y //= 2 print(y // 3) 1. "int" not "in" 2. (x //= 2) instead of (x =// 2) #Remember that when incrementing, operation comes first before equal (e.g. x += 1) 3. I look at the challenge details and it should be (x // 3) instead of (x // 2). Additional: You can just use the "2" right away instead of assigning it to a variable first (which can make the code longer and confusing) I hope this helps. If it is still not solved, please update me. Thanks and Happy Coding!
31st Dec 2020, 6:16 AM
noteve
noteve - avatar
+ 6
Haha36 I tried your code and it worked fine. Somehow, try this print(int(input()//6)
31st Dec 2020, 6:40 AM
Simba
Simba - avatar
+ 4
Its very easy.See the answer below. fruit = int(input()) #your code goes here if fruit > 0: print(int((fruit / 2) // 3))
22nd Jan 2021, 6:00 AM
P. SAI SRI RAM
P. SAI SRI RAM - avatar
+ 3
Seems working fine to me. Try this: x = int(input()) y = x // 2 z = y // 3 print(z)
31st Dec 2020, 6:37 AM
noteve
noteve - avatar
+ 3
Thank you, Simba and Nicko12. I tried your first answer again, both work now
31st Dec 2020, 6:47 AM
Haha36
+ 3
This is the easiest solution to this using C++ #include <iostream> using namespace std; int main() { int fruit,pie; cin>>fruit; cin>>pie; fruit = (fruit/2); pie=fruit/3; cout<<pie; return 0; } I hope this helps
28th Aug 2022, 8:40 PM
Chidera Okeke
Chidera Okeke - avatar
+ 2
Oh yeah, I didnt notice that I use "x" instead of "y" itself. Thanks for the correction :)
31st Dec 2020, 6:24 AM
noteve
noteve - avatar
+ 2
fruit = int(input()) #your code goes here apple = (fruit//2) pie = (apple//3) print(pie) A very basic answer to give you basic understanding
6th Aug 2022, 11:06 AM
Onaga Ebubechukwu
Onaga Ebubechukwu - avatar
+ 2
#include <stdio.h> #include<stdlib.h> int main() { int fruit,count =0,i; scanf("%d", &fruit); fruit =fruit/2; if (fruit<6) { printf ("0"); exit(0); } else if(fruit==6) { printf("2"); exit(0); } else { for(i=3;i<fruit;i=i+3) count++; } printf ("%d",count ); return 0; }
31st Aug 2022, 4:20 AM
SREERAM S
SREERAM S - avatar
+ 2
For the guy that did it for Java I just did this cuz I was to lazy to write out the other variables: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int fruit = input.nextInt(); System.out.println((fruit/2)/3); } }
18th Sep 2023, 7:11 AM
Chris Lagunas
Chris Lagunas - avatar
+ 1
Thanks, I change that. But I still got error for ouput. Line 2 invalid syntax.
31st Dec 2020, 6:20 AM
Haha36
+ 1
fruit = int(input()) halfapples = fruit//2 # half leftover = halfapples//3 # left in 3 halfs print(int(leftover)) #Make things simple
2nd Jan 2023, 6:54 PM
Haris
0
I still got error for output
31st Dec 2020, 6:32 AM
Haha36
0
y=int(input()) y//=2 print(y//3) This is my new code, but I still got error for line 2
31st Dec 2020, 6:34 AM
Haha36
0
Haha36 Maybe you'll write y = y // 2 instead of 'y //= 2' ?
2nd Jan 2021, 3:00 AM
Żab00l
Żab00l - avatar
0
fruit = 26 print(int((fruit / 2) // 3)) переаробовал все что мог,вме что нашел на форумах,все равно дает ошибку.
22nd Nov 2022, 12:46 PM
денис кимаев
денис кимаев - avatar
0
I'll be using ' ' for comments An alternate version I used that might be easier to understand: fruit = int(input()) 'Whatever number of fruits you have in the bowl' #your code goes here apples = fruit // 2 'Apples equals fruit divided by 2' pie = apples // 3 'Pie equals apples divided by 3, as it takes 3 apples to make a pie' print(pie) 'Print the pies you can make with the apples in the bowl' Also, it wants you to print your final variable when it says it needs output.
4th Mar 2023, 2:07 AM
Andre White
Andre White - avatar
0
I basically did this and it's fine x= fruit/2 print (int(x/3))
6th Mar 2023, 8:43 AM
Alican Can
Alican Can - avatar
0
Using java it will work import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int fruit = input.nextInt(); int x = (fruit/2); int y = (x / 3); System.out.println(y); //your code goes here } }
18th May 2023, 8:51 AM
Sarfaraj Bagwan
Sarfaraj Bagwan - avatar
0
#include <stdio.h> #include<stdlib.h> int main() { int fruit,count =0,i; scanf("%d", &fruit); fruit =fruit/2; if (fruit<6) { printf ("0"); exit(0); } else if(fruit==6) { printf("2"); exit(0); } else { for(i=3;i<fruit;i=i+3) count++; } printf ("%d",count ); return 0; }
31st May 2023, 6:00 AM
Ayushi Singh