Computer Science, asked by rakeshchennupati143, 1 year ago

Write a python program.


To take input 2 numbers which are heads of chickens and legs of rabbit respectively. Then return the result of number of chickens , number of rabbits and print them outside the function.


write the whole logic in a function and return only count of chickens and rabbits,and print them out side of function.


If the given input does not make sense to calculate count of chickens and rabbits then return "No solution" and print that outside of the function.


for example consider following Sample input and output statements


Sample - input------------Sample - output

150,400------------------------100,50
3,11-----------------------------No solution
3,12--------------------------------0,3
5,10--------------------------------5,0


The greatest minds gets the BRAINLIEST tag


fiercespartan: In the inputs, if the number of chicken input is 150, then there are 150 chickens right?
fiercespartan: and when the input is 3, the number of chickens is 0.

Answers

Answered by fiercespartan
2

First, we take the input() from the user. The way to do that is just input()

So, here is the first and the second line of the code:

heads = int(input('Enter the number of chicken heads:'))

legs = int(input('Enter the number of legs of rabbits:'))

Now, we know that a chicken has 1 head and a rabbit has 4 legs.

So, the number the user inputs for the number of heads(our first input) is straight the number of chicken.

Hence, we know:

number_of_chicken = int(input('Enter the number of chicken heads:'))

Now, we know that a rabbit has 4 legs. So, if we divide the total number of

Hence, we know:

number_of_rabbit = int(input('Enter the number of rabbit legs:'))/4

But what if, the number of rabbit leg is 3? 3 divided by 4 is not possible. So, this is the case when we return 'No solution' If we just have one 'No solution', we print no solution for the final output.

Alright, so, I would want to make an empty list:

Let's say answers = []

Now, let us add the if-else statement for the rabbits:

number = number_of_rabbit%4

if number > 0:

   answers.append('No solution')

answers.append(number_of_chicken)

Now, we will have the answers in the list 'answers'

If the input is 150,400 , then the answers would be = [150,100]

Now, all we have to do is, print these numbers in one sentence.

print(*(x for x in answers))

Then, the final output would be : 150 100

Now, what if there was a 'No solution'? so let's check.

for x in answers:

   if x == 'No solution':

   answers.clear()

   answers.append('No solution')

Now, the final code would be:

answers = []

answers.append(int(input('Enter the number of chicken heads:')))

number_of_rabbits = int(input('Number of rabbit legs:'))

rabbits = number_of_rabbits/4

number = number_of_rabbits%4

if number > 0 :

   answers.append('No solution')

else:

   answers.append(int(rabbits))

for x in answers:

   if x == 'No solution':

       answers.clear()

       answers.append('No solution')

print(*(x for x in answers),sep = ',')

Hope my answer helps! :)

 


rakeshchennupati143: i'm so sorry there is wrong in my question actually we take heads and legs , but we don't know whether they are chicken's or rabbit's and the we find
rakeshchennupati143: for example we take heads total as 35 and legs 94 so we don't know how many of them are chicken's or rabbit's, so then we calculate total number of chicken and rabbits based on that data he
fiercespartan: could you please post a new question?
rakeshchennupati143: yes i will
fiercespartan: Thank you :)
Answered by Sachinarjun
1

Explanation:

Here is your answer⤵⤵⤵⤵

number_of_chicken = int(input('Enter the number of chicken heads:'))

number_of_chicken = int(input('Enter the number of chicken heads:'))Now, we know that a rabbit has 4 legs. So, if we divide the total number of

number_of_chicken = int(input('Enter the number of chicken heads:'))Now, we know that a rabbit has 4 legs. So, if we divide the total number ofHence, we know:

number_of_chicken = int(input('Enter the number of chicken heads:'))Now, we know that a rabbit has 4 legs. So, if we divide the total number ofHence, we know:number_of_rabbit = int(input('Enter the number of rabbit legs:'))/4

But what if, the number of rabbit leg is 3? 3 divided by 4 is not possible. So, this is the case when we return 'No solution' If we just have one 'No solution', we print no solution for the final output.

Alright, so, I would want to make an empty list:

Let's say answers = []

Now, let us add the if-else statement for the rabbits:

number = number_of_rabbit%4

if number > 0:

   answers.append('No solution')

answers.append(number_of_chicken)

Now, we will have the answers in the list 'answers'.

If the input is 150,400 , then the answers

would be = [150,100]

Now, all we have to do is, print these numbers in one sentence.

print(*(x for x in answers))

Then, the final output would be : 150 100

Now, what if there was a 'No solution'? so let's check.

for x in answers:

   if x == 'No solution':

   answers.clear()

   answers.append('No solution')

Now, the final code would be:

answers = []

answers.append(int(input('Enter the number of chicken heads:')))

number_of_rabbits = int(input('Number of rabbit legs:'))

rabbits = number_of_rabbits/4

number = number_of_rabbits%4

number = number_of_rabbits%4if number > 0 :

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))for x in answers:

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))for x in answers:   if x == 'No solution':

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))for x in answers:   if x == 'No solution':       answers.clear()

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))for x in answers:   if x == 'No solution':       answers.clear()       answers.append('No solution')

number = number_of_rabbits%4if number > 0 :   answers.append('No solution')else:   answers.append(int(rabbits))for x in answers:   if x == 'No solution':       answers.clear()       answers.append('No solution')print(*(x for x in answers),sep = ',')

Hope it will help you✌✌✌

❤❤❤❤❤

Similar questions