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

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


我试过使用for和while循环,并想出了这个:


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")

问题是代码无限运行。


我在循环方面仍然非常缺乏经验,如果有人可以帮助我找到我的错误,我会很高兴。


江户川乱折腾
浏览 193回答 3
3回答

哔哔one

这是一种方法:import randomnum1 = random.randint(-5, 4)num2 = random.randint(-5, 4)counter = 1while 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 += 1print("Took this many iterations: ", counter)没有初始化的例子num1和num2import randomcounter = 1while random.randint(-5, 4) * random.randint(-5, 4):    counter += 1print("Took this many iterations: ", counter)

HUWWW

您生成一对数字,并使用该产品来运行您的循环。由于产品无法更改,因此它是一个无限循环。尝试这个:import randomproduct = 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")

料青山看我应如是

random.randint(a, b)返回一个随机整数N,这样a <= N <= brandom.randrange(a, b)返回一个随机整数N,这样注意和a <= N < b之间的区别<<=如果您想要 -5 到 5(含)范围内的数字,那么您 需要randint(-5, 5)...randrange(-5, 6)randint(-5, 4)以下代码将实现您的目标:import randomiteration_number = 0while True:&nbsp; &nbsp; iteration_number += 1&nbsp; &nbsp; num1 = random.randint(-5, 5)&nbsp; &nbsp; num2 = random.randint(-5, 5)&nbsp; &nbsp; print("Generated number 1: ", num1)&nbsp; &nbsp; print("Generated number 2: ", num2)&nbsp; &nbsp; product = num1 * num2&nbsp; &nbsp; print("Product result: ", product)&nbsp; &nbsp; if product == 0:&nbsp; &nbsp; &nbsp; &nbsp; break # exit the loopprint("We iterated", iteration_number, "times.")虽然上面的代码有效,但它相当难看。这是因为它曾经是一个庞大的脚本,而不是分解成单独的函数。下面的代码实现了相同的最终结果,但更易于阅读、理解和修改:import randomdef gen_once():&nbsp; &nbsp; """&nbsp; &nbsp; generate data once&nbsp; &nbsp; """&nbsp; &nbsp; num1 = random.randint(-5, 5)&nbsp; &nbsp; num2 = random.randint(-5, 5)&nbsp; &nbsp; return num1, num2def test_once(num1, num2):&nbsp; &nbsp; if num1 * num2 == 0:&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; return Falsedef gen_and_test_once():&nbsp; &nbsp; num1, num2 = gen_once()&nbsp; &nbsp; return test_once(num1, num2)def gen_and_test_many():&nbsp; &nbsp; iteration_number = 0&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; iteration_number += 1&nbsp; &nbsp; &nbsp; &nbsp; test_result = gen_and_test_once()&nbsp; &nbsp; &nbsp; &nbsp; if product == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return iteration_numberiteration_count = gen_and_test_many()print("We iterated", iteration_count, "times.")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python