2017-07-29 47 views
1

下面是我的一个简单的骰子滚动程序的代码,程序本身很好,但我的问题是,一旦我滚动(或不),我不能再做任何行动,除了杀死程序,任何和所有的帮助非常感谢。如何多次运行一个程序的目的?

import random 

inp = input("Do you want to roll? Y/N - ").lower() 

if inp=="Y".lower(): 

    print(random.sample(range(1,6),2)) 

if inp=="N".lower(): 

    print("Standing by") 

input('Press ENTER to exit') 

回答

3

如果你想保持程序的运行,循环添加到程序,一旦用户输入“N”

import random 

while True: 
    inp = input("Do you want to roll? Y/N - ").lower() 

    if inp == "y": 
     print(random.sample(range(1,6),2)) 
     continue # ask again 

    if inp == "n": 
     print("Standing by") 
     break # jump to the last line 

input('Press ENTER to exit') 
+1

谢谢,我试图问如何添加一个循环,这是非常有用的! –

1

像AK47这也可以通过函数完成,将只终止。功能的全部重点是重复使用代码

import random 


def roll(): 
    print(random.sample(range(1, 6), 2)) 


while True: 
    inp = input("Do you want to roll? Y/N - ").lower() 
    if inp == "Y".lower(): 
     roll() 
    elif inp == "N".lower(): 
     print("Standing by") 
    else: 
     break 
相关问题