首页 > 解决方案 > 如何在不结束整个程序的情况下退出函数?(Python)

问题描述

我正在尝试对我正在制作的游戏进行重新滚动功能,但是当它询问“你想再次滚动吗?”时,它不会接受否定的答案。循环继续,好像我回答是。我正在使用类型计数器来控制可以重新滚动的次数,因此我尝试将计数器设置为高于允许的数量,但这不起作用。计数器确实允许重新滚动,直到超过允许的数量,所以这似乎不是问题。

for i in range(6):
  #rolls 6 faces of a die and sets them to a "hand"
if counter == 0:
  print("1",hand1,"\n","\nYou earned",resolve1,"resolve")
else:
  print("\n1",hand1,"\n","\nYou earned",resolve1,"resolve")
counter = counter + 1
if counter < 3:
  yn = input("\nDo you want to roll again? (Y/n)\n")
  if yn == "y" or "Y":
    hand1 = []
    resolve1 = 0
  elif yn == "n" or "N":
    counter = 4
else:
  print("\nYou cannot reroll\n")
  counter = 4

顺便说一句,我几乎是个新手,几年前刚在 GCSE 学习计算机科学。

标签: pythonfunction

解决方案


尝试返回一个值或使用break 关键字


推荐阅读