如何让这个乐透节目重复

2024-10-03 15:27:48 发布

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

嗨,我正在创建一个彩票程序,生成6个随机中奖号码,用户将选择什么类型的彩票游戏,他们想要的。我的问题是,如何让这个程序在用户的第一个选择被执行后向他们询问另一个选择

from random import randint   

def GrandLotto(number):               
    print("\nYou Choose GrandLotto 6/55")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,55)    
        print(value)   

def MegaLotto(number):    
    print("\nYou Choose MegaLotto 6/45")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,45)    
        print(value) 

def UltraLotto(number):    
    print("\nYou Choose UltraLotto 6/58")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,58)    
        print(value)    

def SuperLotto(number):    
    print("\nYou Choose SuperLotto 6/49")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,49)    
        print(value)   

def Lotto(number):    
    print("\nYou Choose Lotto 6/42")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,42)    
        print(value)   

def main():    

    print("\tLOTTO GAME")    
    number =0    
    print("[1]=GrandLotto 6/55\n[2]=MegaLotto 6/55")    
    print("[3]=UltraLotto 6/58\n[4]=SuperLotto 6/4")    
    print("[5]=GrandLotto 6/55\n[6]=EXIT")    
    choice = eval(input("What is you Choice : "))    
    if(choice ==1):    
        GrandLotto(number)    
    elif(choice==2):    
         MegaLotto(number)    
    elif(choice==3):    
        UltraLotto(number)    
    elif(choice==4):    
        SuperLotto(number)    
    elif(choice==5):    
        Lotto(number)    
    elif(choice==6):    
        print("Program Closing")                                                                                                                                                           
    else:    
        print("Please Select from the choices above")    

main()    

Tags: innumberforvaluedefrangeprintrandint
2条回答

通过添加while循环并在退出时中断,可以将def main()更改为重复:

def main():    
  while True:
    print("\tLOTTO GAME")    
    number =0    
    print("[1]=GrandLotto 6/55\n[2]=MegaLotto 6/55")    
    print("[3]=UltraLotto 6/58\n[4]=SuperLotto 6/4")    
    print("[5]=GrandLotto 6/55\n[6]=EXIT")    
    choice = eval(input("What is you Choice : "))    
    if(choice ==1):    
        GrandLotto(number)    
    elif(choice==2):    
         MegaLotto(number)    
    elif(choice==3):    
        UltraLotto(number)    
    elif(choice==4):    
        SuperLotto(number)    
    elif(choice==5):    
        Lotto(number)    
    elif(choice==6):    
        print("Program Closing")  
        break                                                                                                                                                         
    else:    
        print("Please Select from the choices above")   

来,让我改进一下你的代码

from random import randint   

def GrandLotto(number):               
    print("\nYou Choose GrandLotto 6/55")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,55)    
        print(value)   

def MegaLotto(number):    
    print("\nYou Choose MegaLotto 6/45")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,45)    
        print(value) 

def UltraLotto(number):    
    print("\nYou Choose UltraLotto 6/58")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,58)    
        print(value)    

def SuperLotto(number):    
    print("\nYou Choose SuperLotto 6/49")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,49)    
        print(value)   

def Lotto(number):    
    print("\nYou Choose Lotto 6/42")    
    print("\nWINNING NUMBERS")    
    for number in range(6):    
        value = randint(0,42)    
        print(value)   

def main():    

    print("\tLOTTO GAME")    
    number =0    
    print("[1]=GrandLotto 6/55\n[2]=MegaLotto 6/55")    
    print("[3]=UltraLotto 6/58\n[4]=SuperLotto 6/4")    
    print("[5]=GrandLotto 6/55\n[6]=EXIT")    
    choice = eval(input("What is you Choice : "))    
    if(choice ==1):    
        GrandLotto(number)    
    elif(choice==2):    
         MegaLotto(number)    
    elif(choice==3):    
        UltraLotto(number)    
    elif(choice==4):    
        SuperLotto(number)    
    elif(choice==5):    
        Lotto(number)    
    elif(choice==6):    
        print("Program Closing")                                                                                                                                                           
    else:    
        print("Please Select from the choices above")    

if __name__ == "__main__"
    while True:
        main()

我添加了__name__ == "__main__",所以这个脚本只有在直接运行时才会执行(如果使用import语句就不会执行),我添加了while True,它基本上是循环的,永远不会结束

相关问题 更多 >