Python-带有随机数的If语句不起作用
我对编程非常陌生,我正试图用python 3.6编写一个程序,生成一个随机数作为“答案”,然后计算机必须猜测x个问题的答案。为了跟踪计算机猜对了多少问题,我创建了一个名为“right”的变量,如果计算机的答案等于猜测值,则在该变量中添加一个。然而,它每次都这样做,即使它是错的。如果这看起来很愚蠢,很抱歉,但是谢谢你的帮助Python-带有随机数的If语句不起作用,python,if-statement,random,Python,If Statement,Random,我对编程非常陌生,我正试图用python 3.6编写一个程序,生成一个随机数作为“答案”,然后计算机必须猜测x个问题的答案。为了跟踪计算机猜对了多少问题,我创建了一个名为“right”的变量,如果计算机的答案等于猜测值,则在该变量中添加一个。然而,它每次都这样做,即使它是错的。如果这看起来很愚蠢,很抱歉,但是谢谢你的帮助 import random def total(x, t): for i in range(t): cor = 0
import random
def total(x, t):
for i in range(t):
cor = 0
gue = 0
n = 0
right = 0
def ans():
cor = random.randint(1,4)
print(cor, 'answer')
def guess():
gue = random.randint(1,4)
print(gue, 'guess')
while n <= x:
ans()
guess()
if cor == gue:
right += 1
n += 1
print(right, n)
随机导入
def总量(x,t):
对于范围(t)内的i:
cor=0
gue=0
n=0
右=0
定义ans():
cor=随机随机随机数(1,4)
打印(cor,‘应答’)
def guess():
gue=random.randint(1,4)
打印(gue,“猜测”)
当n时,我试图通过将ans()
和guess()
函数移到total(x,t)
函数之外来稍微修改代码,因为它们似乎彼此独立。以前的ans
和guess
随机生成cor
和gue
,但不返回它们的值,以便在if
语句中使用,该语句始终使用0
的初始值。现在,通过将返回值保存为cor=ans()
和gue=guess()
,将覆盖cor
和gue
的0
的初始化值
import random
def ans():
cor = random.randint(1,4)
print(cor, 'answer')
return cor
def guess():
gue = random.randint(1,4)
print(gue, 'guess')
return gue
def total(x, t):
for i in range(t):
cor = 0
gue = 0
n = 0
right = 0
while n <= x:
cor = ans()
gue = guess()
if cor == gue:
right += 1
n += 1
print(right, n)
print (total(2,3))
像ans
和guess
这样的嵌套函数可能很有用,但在开始编程时我会避开它。它使您的程序更难理解
下面是我对你想做什么的尝试(如果我理解正确的话!)
随机导入
#玩数字游戏到数字猜谜游戏,允许每玩一个游戏最多猜谜。
def播放(要播放的游戏数,最大猜测数):
num_games=0
猜对了=0
当num_games<要玩的游戏数:
如果猜数(最大猜数):
猜对了+=1
num_games+=1
打印(“\n\n我玩过”,num_games,“games and gusted right”,“gusted_right”,“times”)
#生成一个随机数,试着猜到最大猜测次数。返回数字猜测是否正确。
def猜数(最大猜数):
答案=random.randint(1,4)
打印('\n\n显示猜测号。正确答案是',答案)
num_猜测=0
当num_猜测
我认为您在变量范围方面遇到了问题:cor
和gue
函数内部与外部不同。您的函数没有更改cor
和gue
的值,而是创建了两个仅存在于它们内部的新变量(也称为cor
和gue
)。因此,cor
和gue
始终为0,if语句中的条件始终为true,并且每次都递增rigth
。
要解决此问题,可以将变量作为参数传递给函数。问题来自变量范围。ans方法中定义的变量核不同于for循环中初始化的变量核。
更好的方法是:
随机输入
def ans():
c = random.randint(1, 4)
return c
def guess():
g = random.randint(1, 4)
return g
def total(x, t):
for i in range(t):
n = 0
right = 0
while n <= x:
cor = ans()
gue = guess()
if (cor == gue):
right += 1
print("Answer: %6d Guess: %6d Right: %d" % (cor, gue, right))
n += 1
print(right, n)
def ans():
c=随机的随机数(1,4)
返回c
def guess():
g=随机随机随机数(1,4)
返回g
def总量(x,t):
对于范围(t)内的i:
n=0
右=0
如果cor==gue
,cor
和gue
行中的n始终为零,因此它们始终相等。在ans()
和guess()
函数中更改这些变量只会在该函数中更改它们。有很多方法可以解决这个问题(例如将这些变量声明为非局部变量),但最好的方法是返回值。谢谢你,我拿出了第一部分,其中我将cor和gue定义为0,并在ans()和guess()中使它们成为全局变量。它现在可以工作了,谢谢你的帮助谢谢你这看起来比我原来的好多了!我还在学习,我很感激你的帮助对不起,我说得不清楚,我想做的是一个选择题测试,看看电脑能猜对多少题。X是问题的数量,T是计算机模拟测试的次数。谢谢你的帮助,这是一个有趣的变化谢谢,我把他们搬到外面,现在它工作得很好。谢谢你的帮助!为了感谢那些帮助过你的人的时间/努力,我没有足够高的声誉来展示:(
import random
# Play number_of_games_to_play of the number guessing game allowing a maximum of max_guesses per game played.
def play(number_of_games_to_play, max_guesses):
num_games = 0
guessed_right = 0
while num_games < number_of_games_to_play:
if guess_number(max_guesses):
guessed_right += 1
num_games += 1
print('\n\nI played', num_games, 'games and guessed right', guessed_right, 'times')
# Generate a random number, try and guess it up to max_guesses times. Returns whether the number was guessed correctly or not.
def guess_number(max_guesses):
answer = random.randint(1, 4)
print('\n\nPlaying guess_number. The right answer is', answer)
num_guesses = 0
while num_guesses < max_guesses:
guess = random.randint(1, 4)
print('The computer guesses', guess)
if guess == answer:
print('That is right!')
return True
else:
print('That is wrong')
num_guesses += 1
return False
play(number_of_games_to_play = 5, max_guesses = 4)
def ans():
c = random.randint(1, 4)
return c
def guess():
g = random.randint(1, 4)
return g
def total(x, t):
for i in range(t):
n = 0
right = 0
while n <= x:
cor = ans()
gue = guess()
if (cor == gue):
right += 1
print("Answer: %6d Guess: %6d Right: %d" % (cor, gue, right))
n += 1
print(right, n)