import random
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
b = randomTwo * 2
while b != randomOne:
print("Your first number is not a multiple of the second number!")
x = input('Press Enter to try again.')
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
if randomTwo * 2 == randomOne:
print("Your first number is a multiple of the second number!")
当第二个数字不是第一个数字的倍数时,我无法循环此代码。而且,如何在条件为真时完成程序,即第二个数字是第一个数字的倍数。
答案 0 :(得分:1)
在Python中,与大多数其他编程语言不同,代码缩进很重要。由于缩进已得到修复,因此以下内容应该有效。此外,您需要在while循环中重新计算b
的值,这样循环最终会失败。
import random
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
b = randomTwo * 2
while b != randomOne:
print("Your first number is not a multiple of the second number!")
x = input('Press Enter to try again.')
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
b = randomTwo * 2
# No need to check if it is a multiple since it must be to break the loop
print("Your first number is a multiple of the second number!")
如果您的目标是找到两个数字的组合,其中第一个真正是第二个数字的倍数,您可以尝试这样的事情:
import random
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
b = randomOne / randomTwo
# If b is an integer, then it divided evenly
while b != int(b):
print("Your first number is not a multiple of the second number!")
x = input('Press Enter to try again.')
randomOne = random.randint(1,10)
print(randomOne)
randomTwo = random.randint(1,10)
print(randomTwo)
b = randomOne / randomTwo
if randomTwo * 2 == randomOne:
print("Your first number is a multiple of the second number!")