简体   繁体   English

python中是否有任何方法可以通过在循环结束时要求用户重复一段时间或for循环来进行?

[英]Is there any way in python to repeat a while or for loop by asking the user to do so at the end of the loop?

I'm just starting to learn Python and I'm writing a simple dice rolling program that asks the user for the number of dice and how many sides the dice will have. 我刚开始学习Python,并且正在编写一个简单的骰子滚动程序,该程序向用户询问骰子的数量以及骰子的边数。

So far I have this: 到目前为止,我有这个:

numberOfDice = eval(input("How many dice/die would you like to use? "))
numberOfSides = eval(input("How many sides will each die have? "))

for i in range(1,numberOfDice + 1) :
    roll = random.randint(1,numberOfSides)
    print(roll)

while True :
    replay = input("Would you like to play again?     ")
    if replay.lower() == "yes" :
        numberOfDice = eval(input("How many dice/die would you like to use?     "))
        numberOfSides = eval(input("How many sides will each die have?     "))
        for i in range(1,numberOfDice + 1) :
            roll = random.randint(1,numberOfSides)
            print(roll)
    else :
        break

It works, but it doesn't seem very efficient to me. 它可以工作,但是对我来说似乎并不是很有效。 I'm wondering if there might be a way to ask the user at the end of the first for loop if they want to play again, and, if they say yes, ask them for new values and repeat the for loop again. 我想知道是否有一种方法可以在第一个for循环结束时询问用户是否要再次播放,如果他们说是,请向他们询问新值并再次重复for循环。 Is there any way to do something like this? 有什么办法可以做这样的事情吗?

The general application is a "stay while valid" loop, such as: 通用应用程序是“有效时保持”循环,例如:

replay = True
while replay:
    ... remainder of code
    replay = raw_input("Roll again? (y or n)") == 'y'

Note that this is a simple version, with no error checking: if the user enters a 'y', the program continues; 注意,这是一个简单的版本,没有错误检查:如果用户输入“ y”,则程序继续;否则,程序将继续。 anything else terminates the wonderful world of rolling a douse (never say 'die' :-) ). 其他任何事情都终止了滚转杜兹的绝妙世界(永远不要说'die':-))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 while 循环,询问用户是否要重复程序问题 - while loop, asking user if he wants to repeat the program problem 在循环不以python结束时重复一个函数 - repeat a function while loop not end in python Python 如何在其中添加一个循环,以便在用户将该字段留空时不断询问他们的姓名。 然后一旦输入名称,它就会返回 - Python How do I add a loop to this so that it keeps asking user for their name if they leave the field blank. Then once name is entered it returns Python-如何在没有While循环的情况下连续重复序列,仍然能够随时停止序列 - Python - How do I continuously repeat a sequence without a While loop and still be able to stop the sequence at any time 要求用户在python中使用for循环提供数据 - Asking user to provide data using for loop in python 失败的循环:询问用户是否要继续(Python) - Failed loop: asking the user if they wish to continue (Python) For loop inside while loop in asking user input with conditions - For loop inside while loop in asking user input with conditions Python:如何重复一个虚假的while循环? - Python: How do I repeat a false while loop? 如何在python中重复while循环语句? - How do I repeat a while loop statement in python? 有没有更快的方法来在python中进行while循环? - Is there a faster way to do a while loop in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM