Alice and Bob are playing a game where they each choose a number, and the goal is to see whose number is closer to 50. Write a program that takes two numbers as input and determines which number is closer to 50. If the numbers are equally close to 50, the program should output 0.Company Tags: CapgeminiInput format :The input consists of two integers: the first integer is Alice's number, and the second integer is Bob's number.Output format :The output prints the number closer to 50. If both numbers are equally close to 50, print 0.Refer to the sample output for formatting specifications.Code constraints :1 ≤ input numbers ≤ 100
Question
Alice and Bob are playing a game where they each choose a number, and the goal is to see whose number is closer to 50. Write a program that takes two numbers as input and determines which number is closer to 50. If the numbers are equally close to 50, the program should output 0.Company Tags: CapgeminiInput format :The input consists of two integers: the first integer is Alice's number, and the second integer is Bob's number.Output format :The output prints the number closer to 50. If both numbers are equally close to 50, print 0.Refer to the sample output for formatting specifications.Code constraints :1 ≤ input numbers ≤ 100
Solution
Here is a Python program that solves the problem:
def closer_to_50(a, b):
if abs(50 - a) < abs(50 - b):
return a
elif abs(50 - a) > abs(50 - b):
return b
else:
return 0
# Test the function
print(closer_to_50(45, 55)) # Output: 45
print(closer_to_50(60, 40)) # Output: 40
print(closer_to_50(48, 52)) # Output: 48
print(closer_to_50(50, 50)) # Output: 0
This program defines a function closer_to_50 that takes two numbers as input and returns the one that is closer to 50. If both numbers are equally close to 50, it returns 0. The function uses the abs function to calculate the absolute difference between each number and 50, and then compares these differences to determine which number is closer.
Similar Questions
The output prints the number closer to 50. If both numbers are equally close to 50, print 0.
For this assignment you will write the first part of a program that will play a simple "Guess the Number" game. In the full version of this game (which you will write for this week's project) the program will randomly generate an integer between 1 and 200 inclusive and then will repeatedly ask the user for a guess. If they guess a number that is less than 1 or more than 200 an error message will be printed as a part of this loop. For this lab you will write the piece of the code that checks for this error and ends if the user picks the right number.Sample Output This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Note that the code for this lab includes a "debugging" output that starts with the word "DEBUG: " that tells you the number that was randomly chosen. In the full version of this program that will be removed, but for this incremental piece your lab submission should include it. (You should get used to having this kind of debugging output in your code that isn't part of the final program but is crucial to ensuring that each incremental piece is working properly before you move onto the next part of the program).If the user enters an invalid choice, your code should inform them that their choice was invalid and ask them to guess again.Enter a random seed: 99DEBUG: The number picked is: 188Enter a guess between 1 and 200: 259Your guess is out of range. Pick a number between 1 and 200.That is not the number.Enter a guess between 1 and 200: -1Your guess is out of range. Pick a number between 1 and 200.That is not the number.Enter a guess between 1 and 200: 88That is not the number.Enter a guess between 1 and 200: 188Congratulations! Your guess was correct!I had chosen 188 as the target number.You guessed it in 4 tries.Random numbers: Just like in the FunWithBranching programming assignment, you must use the Random class to generate numbers between 1 and 200. Make sure you are using int values and not doubles for this assignment! Look back at your submitted code for that project and reuse what you can here - reusing code from old assignments isn't only allowed, it's ENCOURAGED! If you've solved a problem once you should remind yourself how you solved it when asked to do something similar!
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
Emma needs your help in deciding which of the two numbers is smaller. Create a program that takes two integers as input, identifies the minimum using a relational operator, and displays it.Input format :The input consists of two space-separated integers.
write a program that performs bitwise AND between 7 and the number given by Alice and prints the result.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.