Python 3.x 随机不返回预期结果
Python 3.x 随机不返回预期结果,python-3.x,Python 3.x,#Python 3.7.2所以我是新手,只想让我的程序在随机数匹配时运行“True”,如果随机数不匹配时运行“False”,这对每个人来说都是非常基本的,但我很慢 def firstSet(): import random for x in range(1): return(random.randint(1,10)) def secondSet():import random for x in range(1): return(random.randint(1,10
#Python 3.7.2
所以我是新手,只想让我的程序在随机数匹配时运行“True”,如果随机数不匹配时运行“False”,这对每个人来说都是非常基本的,但我很慢
def firstSet():
import random
for x in range(1):
return(random.randint(1,10))
def secondSet():import random
for x in range(1):
return(random.randint(1,10))
def view():
return(secondSet(), firstSet())
def theMatch():
if secondSet() == firstSet():
return(True, view())
else:
return(False, view())
print(theMatch())
theMatch()
我希望输出类似于
(True,(5,5))
或(False,(2,3))
,但实际输出通常类似于(True,(10,2))
或(False,(7,7))
完全随机。我知道有很多不同的方法可以用更简单的方法编写这个程序,但我只是想知道我哪里出错了。谢谢欢迎来到堆栈溢出
代码无法正常运行的原因是,您实际上多次重新计算该“随机”值,包括在检查它们是否匹配之后,以及在打印之前重新计算该值
让我们稍微浏览一下您的代码:调用match()时,会发生以下情况:
# Grab the random library
import random
# Create a function to calculate a random number
def create_random_number():
# No need for a loop, just use x to denote a random number between 1 and 10
x = random.randint(1,10)
# Return that random number
return x
# This is your main function.
# This is sometimes called a "driver" program
def main():
# Store a random value in the first_val variable
first_val = create_random_number()
# Store a random value in the second_val variable
second_val = create_random_number()
# If they added up, print out what they were, and that it was a true match
if first_val == second_val:
print(first_val, " and ", second_val, " match, this is TRUE")
# If not, print out what they were, and that it was not a match
else:
print(first_val, " and ", second_val, " do not match, this is FALSE")
# Actually execute your main function
main()
希望这有助于回答您的问题。简而言之,这是因为您正在比较两个值,然后获取不同的随机值以打印出来(这有助于按顺序遍历这些函数的逻辑以及正在发生的事情)
下面是您的代码注释,解释发生了什么:
def firstSet():
import random
for x in range(1):
return(random.randint(1,10))
def secondSet():import random
for x in range(1):
return(random.randint(1,10))
def view():
return(secondSet(), firstSet())
def theMatch():
# This pair here are the first and second random integers
if secondSet() == firstSet():
# Now, view() gets called which returns the functions, therefore returning a DIFFERENT set of random integers than the ones being tested
return(True, view())
else:
# Now, view() gets called which returns the functions, therefore returning a DIFFERENT set of random integers than the ones being tested
return(False, view())
print(theMatch())
theMatch()
下面是对您的代码的一个修复,它似乎对我有效:
def firstSet():
import random
for x in range(1):
return(random.randint(1,10))
def secondSet():
import random
for x in range(1):
return(random.randint(1,10))
def theMatch():
firstVal = firstSet()
secondVal = secondSet()
if secondVal == firstVal:
# Now, view() gets called which returns the functions, therefore returning a DIFFERENT set of random integers than the ones being tested
return(True, secondVal, firstVal)
else:
# Now, view() gets called which returns the functions, therefore returning a DIFFERENT set of random integers than the ones being tested
return(False, secondVal, firstVal)
print(theMatch())
theMatch()
您的代码正在多次重新计算随机值。。。。