gpt4 book ai didi

python - 如何从列表中随机调用函数?

转载 作者:行者123 更新时间:2023-12-05 05:38:59 27 4
gpt4 key购买 nike

我是 Python 初学者,我休息了一个月,今天我回来了。在我正在阅读的学习 Python 的书中,作者要求我们记住真值表。我想让它变得更有趣,所以我想出了一个想法,尝试编写一个脚本,从列表中随机询问(我写的)问题,我可以用“真”或“假”来回答。问题是我在代码中使用的很多东西我还没有从书中完成,所以我使用互联网来帮助我。

这是我想出的:

print("Here are some questions for you, answer true or false.")
input("Press 'Enter' if you would like to continue.")

def a():
answer = input("1) not False: ")
print("Correct!") if answer == 'true' else print("Wrong!")

def b():
answer = input("2) not True: ")
print("Correct!") if answer == "false" else print ("Wrong!")

def c():
answer = input("3) True or False: ")
print("Correct!") if answer == "true" else print ("Wrong!")

import random

questions = [a(), b(), c()]

print(random.sample(questions, 3))

问题更多,我只用了三个来测试代码是否有效。到目前为止一切顺利,如果不是因为我似乎无法从我的列表中调用随机函数的话。

这是我的输出: output

另外,[None, None, None] 是什么意思?

我知道使用我什至还没有研究过的东西不是一个好主意,但我对此感到非常兴奋。

TIA!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com