Python 3 Y/N循环问题

2024-06-16 17:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,我只是在玩一些非常简单的小程序来了解它,所以最好让任何解释都非常简单,哈哈

我正在制作一个小程序,询问你是否想掷骰子,掷骰子,给出答案,然后询问你是否想再次掷骰子。 我很难弄清楚以下问题(从控制台复制):

What is your name: Nasicus

Greetings Nasicus!

Would you like to roll the dice? [Y/N]? : Y

Let's do this!

Rolling...

You rolled a 3!

Do you want to roll again? [Y/N]?: Y

Yahoo!

Would you like to roll the dice? [Y/N]? : N

Oh, Okay. Maybe next time.

Would you like to roll the dice? [Y/N]? : N

Oh, Okay. Maybe next time.

Process finished with exit code 0

如您所见,当您在关闭前选择N时,它会提示两次。 我可能错过了一些非常简单的东西,因此有谁能建议我如何A.停止提示两次,或者(最好是为了简单起见)B.停止询问在您选择再次掷骰子后是否要掷骰子,然后直接从Let's do this!行开始

这是我的代码,任何关于如何让事情更整洁/更具Python风格的建议都会得到赞赏!我很欣赏time.sleep()可能看起来有点凌乱,但我确实喜欢它在运行时的速度:

import random
import time

def diceroll():
    while True:
        diceyn = input ("Would you like to roll the dice? [Y/N]? : ")
        if diceyn == "Y":
            print ("Let's do this!")
            time.sleep(0.5)
            print ("Rolling...")
            time.sleep(1)
            rand = random.randint(1, 6)
            print ('You rolled a ',rand,'!', sep='')
            time.sleep(0.5)
            again = str(input("Do you want to roll again? [Y/N]?: "))
            if again == "Y":
                print ('Yahoo!')
                time.sleep(0.5)
                diceroll()
            else:
                time.sleep(0.3)
                print ('Okay, bye!')
                break
        elif diceyn == "N":
            print ("Oh, Okay. Maybe next time.")
            break

input_name = input ("What is your name: ")
print ("Greetings ",input_name,"!", sep='')
time.sleep(1)

diceroll()

感谢您抽出时间,我期待着了解更多:D


Tags: thetonameyouinputtimesleepdice
3条回答

我只需要在diceroll()函数中将实际的滚动分解为一个单独的函数,这样就不会混淆路径

import random
import time

def diceroll():
    def rollIt():
        time.sleep(0.5)
        print ("Rolling...")
        time.sleep(1)
        rand = random.randint(1, 6)
        print ('You rolled a ',rand,'!', sep='')
        time.sleep(0.5)

    while True:
         diceyn = input ("Would you like to roll the dice? [Y/N]? : ")
         if diceyn == "Y":
            print ("Let's do this!")
            rollIt()
            again = str(input("Do you want to roll again? [Y/N]?: "))
            if again == "Y":
                print ('Yahoo!')
                rollIt()
            else:
                time.sleep(0.3)
                print ('Okay, bye!')
                break
        elif diceyn == "N":
            print ("Oh, Okay. Maybe next time.")
            break

input_name = input ("What is your name: ")
print ("Greetings ",input_name,"!", sep='')
time.sleep(1)

diceroll()

您正在调用dicerollrecursively

if again == "Y":
   print ('Yahoo!')
   time.sleep(0.5)
   diceroll()

因此,您可以调用diceroll(),然后在用户被询问时调用

Do you want to roll again

您再次调用diceroll()

下面是正在发生的事情。您有一个顶级diceroll()

diceroll()

然后在它下面有另一个diceroll(),如下所示:

diceroll()

——diceroll()

然后在它里面还有另一个diceroll()

diceroll()

——diceroll()

---diceroll()

当您调用break语句时,您所做的只是中断内部diceroll()循环,而不是调用它的循环

第三排的休息会让你

diceroll()

——diceroll()

问题出在代码的这一部分:

if again == "Y":
    print ('Yahoo!')
    time.sleep(0.5)
    diceroll()

您正在递归调用diceroll()函数,因此当递归调用最终完成时,当前调用的迭代仍在继续

您已经处于while True循环中,因此甚至不需要递归调用。把它拿出来,让循环继续

相关问题 更多 >