0

This code keep repeating the same sets of numbers that add up to a specific number. ex: 31+ 69 = 100. This will be repeated many times among other possibilities. What's the matter with my code ? enter image description here

Thanks. Here it is:

import random

target_number = int(input('Input a number and we will return a list of two numbers that equate to this number'))

r1 = range(1,target_number)
r2 = range(1,target_number)

while True:
    for i in range(24):
        num1 = random.choice(r1)
        num2 = random.choice(r2)
        if num1 + num2 == target_number:
            print(f"solved: {num1}+{num2} = {target_number}")
6
  • What's the for i in range(24) for? Do you want to print 24 pairs of numbers? Commented Aug 28, 2022 at 1:45
  • The outer while True loop has no way to exit. So it will run forever. What were you expecting to happen? Commented Aug 28, 2022 at 1:46
  • 1
    Using random numbers is highly inefficient. You might quickly get unique pairs in the beginning, but it will get increasingly harder to get all pairs. You want to generate combinations Commented Aug 28, 2022 at 1:46
  • 1
    You could set num1 to be random and just make num2 = target - num1... Commented Aug 28, 2022 at 1:49
  • Yes, that was my also my comment to one of the answers ;) Commented Aug 28, 2022 at 1:54

4 Answers 4

1

As noted by HuLu, while True will run forever. The for loop with i could be removed as follows:

import random

target_number = int(input('Input a number and we will return a list of two numbers that equate to this number'))

r1 = range(1,target_number)
r2 = range(1,target_number)

while True:
    num1 = random.choice(r1)
    num2 = random.choice(r2)
    if num1 + num2 == target_number:
        print(f"solved: {num1}+{num2} = {target_number}")
        break
Sign up to request clarification or add additional context in comments.

Comments

1

If you're looking to find all pairs that add up to your number, try the following:

def find_factors(x):
    for i in range(x): # loop through all numbers (until x)
        if i <= x-i: # this is to avoid printing duplicate pairs, e.g. (2,3) and (3,2)
            print([i,x-i]) # print i, and i subtracted from x

This function will take x as an argument and return all pairs of integers that add up to x.

E.g. calling find_factors(5) will return:

[0, 5]
[1, 4]
[2, 3]

13 Comments

^^ Now that I read your question again, it looks like you were interested in finding a single (random) pair only. As @mozway pointed out -- you may need all combinations, so my code will help you get them.
Much better approach than the original one (+1), still one could do even better by directly generating the pairs of numbers that add up to 100. (100+0, 99+1, 98+2...) without any test ;)
Without the "while true" it only gives me 1 output. I want a list of outputs but I don't want to print dublicate pairs.
@Mr.Green pairs = [(i, 100-i) for i in range(51)] that's it.
@mozway -- we may want to replace 100 with a generic x: [(i, x-i) for i in range(int(x/2+1))]
|
0

while True means "repeat forever", you have to put and end to the loop:

import random

target_number = int(input('Input a number and we will return a list of two numbers that equate to this number'))

r1 = range(1,target_number)
r2 = range(1,target_number)

cont = True
while cont:
    for i in range(24):
        num1 = random.choice(r1)
        num2 = random.choice(r2)
        if num1 + num2 == target_number:
            print(f"solved: {num1}+{num2} = {target_number}")
            cont = False

1 Comment

I have a list of possible numbers adding to eg: 100, but most repeat themselves at least 15 times, the same 31+69 = 100. How to only have 1 (31+69 =100) ? thanks
0

use break as soon as you find numbers with a sum of target_number in if statement.Otherwise this while loop will run forever.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.