我如何计算用户猜测正确序列的尝试次数

  • 本文关键字:何计算 计算 用户 python count
  • 更新时间 :
  • 英文 :


我想统计一下用户尝试猜测正确颜色序列的次数。我已经被卡住了很长一段时间,因为我试图添加计数函数,但它一直被卡住在0。我是python的新手,所以非常感谢任何帮助(我不得不删除代码中一些不相关的部分,因为我无法将其放入框中(在compare_list部分(

import random
colours = ['PINK', 'BLUE', 'YELLOW', 'RED']
random_colours = []
colours_input = []
#handles the users input and stores in a list (colours_input)
def user_input():
i = 1
while i < 5:
a = input('Please enter your selected colours in sequence, colour ' + str(i) + ': ').upper()
while a not in colours:
print('Please only type the colours in the range (PINK, BLUE, YELLOW, RED) ')
a = input('Please enter a valid colour ' + str(i) + ': ').upper()

colours_input.append(a)
i+=1
print('Your selected colour sequence is: ' + str(colours_input))
#Automatically generate four random colours for the user to guess    
def randomize():
for i in range (0,4):
random_colours.append(random.choice(colours))
#To check 2 variables: Correct colour in the correct place and correct colour but in the wrong place
def compare_list():
correct_place = 0
if random_colours[0] == colours_input[0]:
colour_1 = True
correct_place = correct_place + 1
else:
colour_1 = False
if random_colours[1] == colours_input[1]:
colour_2 = True
correct_place = correct_place + 1
else:
colour_2 = False
if random_colours[2] == colours_input[2]:
colour_3 = True
correct_place = correct_place + 1
else:
colour_3 = False
if random_colours[3] == colours_input[3]:
colour_4 = True
correct_place = correct_place + 1
else:
colour_4 = False
print('Correct colour in the correct place: ' + str(correct_place))
while correct_place == 4:
print('Congratulations! You are a master mind')
break
else:
colours_input.clear()
user_input()
compare_list()

您将需要一些驱动程序代码,作为程序的入口点,并协调函数的调用方式。目前,您正在compare_list()函数中执行此操作,只需将此代码移动到一个新函数即可(并对其进行一些更改,while循环结构存在一些错误(。

通常,此代码被放置在main函数中。在你的情况下,它可能看起来像:

def main():
random_colours = []
colours_input = []
randomize() # you forgot to call this in your code
while True:
user_input()
if compare_list():
print('Congratulations! You are a master mind')
break # exit the program
colours_input.clear()

然后,我们只需要重构compare_list()函数,使其只比较列表,并返回比较结果(我只返回一个表示比较是否成功的布尔值,但也可以返回correct_place值(。

def compare_list():
correct_place = 0
if random_colours[0] == colours_input[0]:
colour_1 = True
correct_place = correct_place + 1
else:
colour_1 = False
if random_colours[1] == colours_input[1]:
...
print('Correct colour in the correct place: ' + str(correct_place))
return correct_place == 4 # returns True, if correct_place equals 4, False otherwise

现在,在我们的main函数中,我们可以计算每个用户尝试运行这个主循环的次数:

def main():
random_colours = []
colours_input = []

attempts = 0
while True:
attempts += 1
user_input()
randomize()
if compare_list():
print('Congratulations! You are a master mind')
print('It only took you {} attempt{}!'.format(attempts, 's' if attempts > 1 else "")
break # exit
colours_input.clear()
random_colours.clear()

现在,我们只需调用文件中的main()函数来运行驱动程序代码。通常,这是在if块中完成的,该块只有在脚本直接运行而不是由其他东西导入时才运行:

...
def compare_list():
...
def main():
...
if __name__ == "__main__":
main()

(你可以在这里阅读关于这个练习的内容:if __name__ == “__main__”:是做什么的?(

最后一个重构是减少全局变量的使用。您应该致力于使函数成为对输入和返回输出进行操作的代码单元。设计函数以使它们变异全局变量来产生结果是一种反模式。

这里有一种重构方法:

import random
colours = ('PINK', 'BLUE', 'YELLOW', 'RED') # this is okay since it is a constant. We can make it an immutable tuple to clearly indicate that this is read-only.
# remove the following global variables
# random_colours = []
# colours_input = []
#handles the users input and stores in a list (colours_input)
def user_input():
colours_input = [] # define the variable here. We create a new list that this function will populate, then return it
for i in range(5):
...
print('Your selected colour sequence is: ' + str(colours_input))
return colours_input # return the populated list
def randomize():
random_colours = [] # define the variable here. Similar to above.
for i in range (0,4):
random_colours.append(random.choice(colours))

return random_colours # return it to the caller
# we take in the lists to compare as parameters to the function, rather then getting them from the global scope
def compare_list(colours_input, random_colours):
... # (everything else is the same)
def main():
random_colours = randomize()
# colours_input = []
attempts = 0
while True:
attempts += 1
colours_input = user_input()
if compare_list(colours_input, random_colours): # pass the returned lists in as arguments to this function call
print('Congratulations! You are a master mind')
print('It only took you {} attempt{}!'.format(attempts, 's' if attempts > 1 else "")
break # exit the program
# no need to clear this anymore. We get a new list from each call to user_input()
# colours_input.clear()

最新更新