我有兴趣在自己喜欢的文字游戏中加入“运气”系统。将会有某些if / else语句,并且会根据用户的选择为变量分配一个数字,我想稍后将所有的运气加起来,以便在其他一些语句中使用。
我会最好把每个运气if / else对话放到自己的函数中,然后再尝试将所有函数与total_luck函数一起添加吗?
目前正在考虑这样的事情,但这是最好的方法吗?
def luckquestion_1():
question = input("What will you do? ").lower()
if question in "a":
return 10
elif question in "b":
return 4
elif question in "c":
return 1
def luckquestion_2():
question = input("What will you do? ").lower()
if question in "a":
return 10
elif question in "b":
return 4
elif question in "c":
return 1
def luckquestion_3():
question = input("What will you do? ").lower()
if question in "a":
return 10
elif question in "b":
return 4
elif question in "c":
return 1
luck1 = luckquestion_1()
luck2 = luckquestion_2()
luck3 = luckquestion_3 ()
luck_total = luck1 + luck2 + luck3
答案 0 :(得分:0)
您可以使用一个函数并传入参数:
<!-- remove onclick="playSound()" -->
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<audio data-key="65" src="https://www.w3schools.com/html/horse.ogg"></audio>
这假设每次运气的返回值都是一致的。