[英]Reuse the same function until dice roll is higher
你能帮我处理我的代码吗? 我对 python 和一般编码非常熟悉。 当随机生成的数字大于“1”时,我的代码运行良好。 如何在生成的数字大于“1”之前重新运行函数?
我的代码是:
from random import randint
def dice_roll_input():
'''
Askes the user to type "roll" to run the function that
simulates rolling the dice.
'''
print('Roll more then "1" to kill the python.\n')
dice_conf = input('Type "roll" to roll the dice\n')
if dice_conf == 'roll':
return
if dice_conf != 'roll':
dice_roll_input()
def roll_dice():
'''
Simulates rolling the dice and returning the result.
'''
random_dice = randint(1,6)
return random_dice
def kill_python(dice):
'''
Based on the dice roll, either python gets killed or
it dodges the attack and user needs to roll again.
'''
if dice == 1:
print(f'You rolled: {dice} \n')
print('Python dodged your attack. Try again.\n')
else:
print(f'You rolled: {dice} \n')
print('You killed the python!')
def attack_python():
'''
Function to loop through dice rolls until dice>1.
'''
dice = 1
while dice == 1:
dice_roll_input()
dice = roll_dice()
kill_python(dice)
def main():
"""
Run all program functions
"""
attack_python()
main()
您可以添加一个 while 循环。 while 条件:如果条件为真,则命令重复代码块并在条件为假时停止。
例如下一个代码将运行 3 次并停止一个 i=4 因为 i<4 == False
i=0
while i<4:
print(f"{i=}")
i+=1
在你的情况下,这样的事情应该有效
def main():
"""
Run all program functions
"""
dice=1
while dice==1:
dice_roll_input()
dice = roll_dice()
kill_python(dice)
在 Python 中,有两个工具可用于让一段代码运行未知次数。
第一个方法是 while 语句,如下所示:
while some_logical_value:
do_some_code()
do_more_code()
do_other_code()
当解释器遇到 while 语句时,它会像 if 一样评估它给出的任何值。 如果该值被认为是 false,则跳过缩进块中的代码,如果该值被认为是 true,则运行。 但是,如果运行缩进块中的代码,解释器会在完成后立即返回到 while 语句。 在示例中,一旦 do_more_code() 完成,解释器就会跳转到 while 行,而不是像使用 if 语句那样继续执行 do_other_code()。
第二个选项是递归,看起来像这样:
def main():
do_some_code()
if some_logical_value:
main()
在这里,我们手动指示解释器将 go 返回到我们代码的顶部。 这有它的好处,但它也有缺点 - 它通常较慢,并且倾向于使用更多 memory。此外,Python 只允许你 go 在默认抛出错误之前深入 1000 个递归级别 - 所以如果你有一些自然超过这个(比如应该在游戏中每秒运行的代码或找到第 1500 个斐波那契数的代码),你就会遇到问题。 与此同时,While 循环可以无限期地运行 go。
有关递归的更多讨论,请参阅此答案。
晚上好。 我找到了一种方法,可以让您的代码在骰子落在 1 时平稳运行。主要问题出在 kill_python function 的 if 语句中,您只调用了 dice_roll_input function。仅调用此 function 的问题是当用户再次输入 roll,程序只运行返回 function 并结束,因为当骰子结果为 1 时您不使用其他两个函数。要在骰子落在 1 后完全循环代码,您需要调用其他两个函数分别(结合 dice_roll_input 函数)或将 dice_roll_input function 替换为“main”function。两种方法都可以正常工作,但我建议使用“main”function 来压缩代码。 我对您的代码的演绎可以在下面找到。
from random import randint
def dice_roll_input():
'''
Askes the user to type "roll" to run the function that
simulates rolling the dice.
'''
print('Roll more then "1" to kill the python.\n')
dice_conf = input('Type "roll" to roll the dice\n')
if dice_conf == 'roll':
return
if dice_conf != 'roll':
dice_roll_input()
def roll_dice():
'''
Simulates rolling the dice and returning the result.
'''
random_dice = randint(1,6)
return random_dice
def kill_python(dice):
'''
Based on the dice roll, either python gets killed or
it dodges the attack and user needs to roll again.
'''
if dice == 1:
print(f'You rolled: {dice} \n')
print('Python dodged your attack. Try again.\n')
main()
else:
print(f'You rolled: {dice} \n')
print('You killed the python!')
def main():
"""
Run all program functions
"""
dice_roll_input()
dice = roll_dice()
kill_python(dice)
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.