如何使此乐透程序可重复

时间:2019-11-30 02:30:30

标签: 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()    

2 个答案:

答案 0 :(得分:1)

在这里,让我改进您的代码

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,它基本上是循环的,永远不会结束

答案 1 :(得分:0)

您可以通过添加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")