+ 1

I want to add in my program that when I put 4 random numbers that tell me which of those 4 is the closest to 10

I want to add in my program that when I put 4 random numbers that tell me which of those 4 is the closest to 10 https://code.sololearn.com/cKt6DiJZnO52/?ref=app

26th Dec 2022, 3:28 AM
Gaston
Gaston  - avatar
7 Réponses
+ 5
Gaston , we can use 2 steps to get a result: from random import sample # (1) random_nums = sample(range(0, 101),k=4) # (1) print(min(random_nums, key = lambda x: abs(x - 10))) # (2) (1) we can use the random sample() function that can generate *k* random and unique numbers in one go. the result is a list. (2) to get the smallest distance to 10 for each random value, we can use the min() function, combined with the key= argument. the argument is a lambda function, that calculates the absolute difference to 10 for each random number.
26th Dec 2022, 12:29 PM
Lothar
Lothar - avatar
+ 4
Omanshu , there are typos in the code: rand_nos=[5,14,19,4] closest_from_ten=rand_nos[0] from no in rand_nos: # should be: for ... If abs(no-10)<abs(closest_from_ten-10): # *if* should be lower case closest _from_ten=no #there is a space in the variable name
27th Dec 2022, 4:43 PM
Lothar
Lothar - avatar
+ 2
What you have to find is basically the number with the 'minimum distance from 10'. So you can use the min() function and pass the additional 'key' argument to find the 'minimum distance'. The 'key' argument is a function. The element of the iterable (list) is first "transformed" to another number using the function, and then the minimum is returned according to the values they were transformed into. You might get lost in the words, so here is an example def square(n): return n * n l = [-5, 2, 4, 3] min_square = min(l, key=square) Now, min_square will be equal to the number whose square is the smallest, i.e. 2 See if you can apply this to your own problem.
26th Dec 2022, 4:05 AM
XXX
XXX - avatar
+ 2
The steps are simple: 1) Subtract all the numbers by 10 and use the absolute value using the abs() function 2)whosever absolute value to smallest is the closest no. From 10 Code: rand_nos=[5,14,19,4] closest_from_ten=rand_nos[0] from no in rand_nos: If abs(no-10)<abs(closest_from_ten-10): closest _from_ten=no
27th Dec 2022, 12:34 PM
Omanshu
Omanshu - avatar
+ 1
If I understand the question right to find the closest number to 10 or any given number, another method would be: b = [4, 11.2, -9] conv = [abs(10 - num) for num in b] closest = b[conv.index(min(conv))] What this does is minus the target(10) by each num and gets the absolute value, making the minimum value the closest to the target. By calling min.index you get both the index of the closet value and the number itself. You could also get the furthest by calling max.
26th Dec 2022, 7:26 PM
Christopher Smith
Christopher Smith - avatar
0
import random jug = input().split(",") jug = [int(i) for i in jug] if jug[0] < 0 or jug[0] > 100: print("elige entre el 0 al 100") jug1 = random.randint(0, 101) jug2 = random.randint(0, 101) jug3 = random.randint(0, 101) print(jug[0], jug1, jug2, jug3) promedio = print((jug[0] + jug1 + jug2 + jug3)/4) jugadores = jug[0], jug1, jug2, jug3
26th Dec 2022, 4:33 AM
Deni Herdian Rusviana
Deni Herdian Rusviana - avatar
0
This is my code but not related to yours, it will search for the nearest number to 10. # get user input numbers = [int(x) for x in input("Enter four numbers separated by commas: ").split(',')] # calculate the difference between each number and 10 differences = [abs(10 - x) for x in numbers] # find the smallest difference smallest_difference = min(differences) # find which number corresponds to the smallest difference closest_number = numbers[differences.index(smallest_difference)] # print the closest number print("The number closest to 10 is", closest_number)
26th Dec 2022, 4:41 AM
Deni Herdian Rusviana
Deni Herdian Rusviana - avatar