如何将两个随机生成的数字相乘直到它们等于0?

时间:2019-06-17 23:39:18

标签: python while-loop

我需要在-5到5范围内随机生成2个数字,然后将它们相乘直到得到乘积为0的迭代。然后打印获得结果所需的循环次数。

我尝试使用forwhile循环,并想到了:

import random
num1 = random.randint(-5, 4)
num2 = random.randint(-5, 4)
print("Generated number 1: ", num1)
print("Generated number 2: ", num2)
product = num1 * num2
print("Product result: ", product)
while product != 0:
    print("Failed iteration")
else:
    print("Successful iteration")

问题在于代码无限运行。

我仍然缺乏循环经验,如果有人可以帮助我找到我的错误,我会很乐意。

4 个答案:

答案 0 :(得分:1)

这里是一种方法:

import random

num1 = random.randint(-5, 4)
num2 = random.randint(-5, 4)
counter = 1

while num1 * num2: 
#while will iterate until the condition is not true, in python 0 is equivalent to false.
    num1 = random.randint(-5, 4)
    num2 = random.randint(-5, 4)
    counter += 1

print("Took this many iterations: ", counter)

未初始化num1num2的示例

import random

counter = 1

while random.randint(-5, 4) * random.randint(-5, 4):
    counter += 1

print("Took this many iterations: ", counter)

答案 1 :(得分:0)

您的错误是您仅计算了一个“迭代”并将其结果存储在product中。如果不重做计算,则不会改变。

基本上,您需要将计算放入循环中。您可以使用原始的while product != 0,但随后必须计算循环外的第一个乘积和循环内的其他乘积,从而导致代码重复。因此,我切换到了无限循环(while True),从这里我们break成功了。

import random

while True:
    num1 = random.randint(-5, 4)
    num2 = random.randint(-5, 4)
    print("Generated number 1: ", num1)
    print("Generated number 2: ", num2)
    product = num1 * num2
    print("Product result: ", product)

    if product == 0:
        print("Successful iteration")
        break
    else:
        print("Failed iteration")

答案 2 :(得分:0)

您生成一对数字,并使用该乘积运行循环。由于产品无法更改,因此这是一个无限循环。试试这个:

import random

product = 10000  # Dummy value to get into the loop.

while product != 0:
    num1 = random.randint(-5, 4)
    num2 = random.randint(-5, 4)
    print("Generated number 1: ", num1)
    print("Generated number 2: ", num2)
    product = num1 * num2
    print("Product result: ", product)
    if product != 0:
        print("Failed iteration")

else:
    print("Successful iteration")

答案 3 :(得分:0)

random.randint(a, b)返回一个随机整数N,使得a <= N <= b
random.randrange(a, b)返回一个随机整数N,使得a <= N < b
请注意<<=

之间的区别

如果您希望数字介于-5到5(含)之间,则需要...
randint(-5, 5)
 或
randrange(-5, 6)
但不是
 randint(-5, 4)

以下代码将实现您的目标:

import random
iteration_number = 0
while True:
    iteration_number += 1
    num1 = random.randint(-5, 5)
    num2 = random.randint(-5, 5)
    print("Generated number 1: ", num1)
    print("Generated number 2: ", num2)
    product = num1 * num2
    print("Product result: ", product)
    if product == 0:
        break # exit the loop
print("We iterated", iteration_number, "times.")

尽管上面的代码可以工作,但是却很难看。这是因为它曾经是大型脚本,而不是分解为单独的函数。以下代码可以达到相同的最终结果,但更易于阅读,理解和修改:

import random

def gen_once():
    """
    generate data once
    """
    num1 = random.randint(-5, 5)
    num2 = random.randint(-5, 5)
    return num1, num2

def test_once(num1, num2):
    if num1 * num2 == 0:
        return True
    return False

def gen_and_test_once():
    num1, num2 = gen_once()
    return test_once(num1, num2)

def gen_and_test_many():
    iteration_number = 0
    while True:
        iteration_number += 1
        test_result = gen_and_test_once()
        if product == 0:
            return iteration_number

iteration_count = gen_and_test_many()
print("We iterated", iteration_count, "times.")