2017-04-06 3 views
1

기능을 호출하는 데 문제가 있습니다. 생성 된 모든 함수는 잘 동작합니다. 메뉴에서 함수를 호출하는 데 문제가 있습니다. 나는 아직도 파이썬에 대해 아주 새로운데, 이해가되지 않는 것을 보게된다면 나에게 말해줘. umutto에 의해 제안메뉴에서 기능을 호출 할 수 없습니다.

#function for lottery generator 
def lottery_number_generator(): 
    import random 
    lottoNumber = [] 

    for i in range(0,7): 
     randNum = random.randint(0,9) 
     lottoNumber.append(randNum) 

    for i in range(0,7): 
     print lottoNumber[i], 

#function for number analysis 
def number_analysis(): 
    total = 0.0 
    average = 0.0 
    numberInput = 0.0 
    numbers = [] 

    print("Enter in a series of 20 numbers\n") 

    for i in range(20): 
     numberInput = input("Enter your %s number: " % (i+1)) 
     numbers.append(numberInput) 
     total += numberInput 
    average = total/20 

    print ("\nThe highest number is %.2f: " % max(numbers)) 
    print ("The lowest number is %.2f: " % min(numbers)) 
    print ("Your total is %.2f: " % total) 
    print ("Your average is %.2f: " %average) 

#function for rainfall calculator 
def rainfall(): 
    totRain = 0.0 
    average = 0.0 
    totalRain = 0.0 
    months = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 
    rainFall = [] 
    for i in range(0,12): 
     monthRain = input("Enter rainfall inches for %s: " % months[i]) 
     rainFall.append(monthRain) 
     totalRain += monthRain 

    average = float(totalRain/12) 

    for i in range(0,12): 
     print '' 
     print("Rainfall for %s is %i" %(months[i],rainFall[i])) 

    print ("\nThe total rainfall was %.2f: " % totalRain) 
    print ("The average rainfall is %.2f" % average) 
    print ("The highest amount of rainfall was %.2f "% max(rainFall)) 
    print ("The lowest amount of rainfall was %.2f "% min(rainFall)) 

#function for total sales 
def total_sales(): 
    day = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] 
    dailySales = [] 
    sales = 0.0 
    totalSales = 0.0 

    for i in range(0,7): 
     sales = input("Enter sales for %s " % day[i]) 
     dailySales.append(sales) 
     totalSales += sales  

    for i in range(0,7): 
     print ("Sales for %s is %.2f:" % (day[i],dailySales[i])) 
    print ("For a total of %.2f: " % totalSales) 
def menu(): 
    print ("1. Play lottery number generator.\n2. Play number analysis.\n3. Play rainfall calculator.\n4. Play total sales calculator.\n5. Exit/Quit.") 
    input() 
def main(): 
    while True: 
     if menu() == 1: 
      lottery_number_generator() 
     elif menu() == 2: 
      number_analysis() 
     elif menu() == 3: 
      rainfall() 
     elif menu() == 4: 
      total_sales() 
     elif menu() == 0: 
      quit 
     else: 
      print ("\n Not a valid choice try AGAIN.") 

#start of program 


print ("Welcome, Please select which HW assignment you would like to view:\nPlease select which HW assignment to play by pushing the number.\n") 

menu() 
main() 
+0

당신은 메뉴 함수에서'return()'을하고 싶습니다. 또한 코드의 두 번째 마지막 줄에서'menu()'를 호출 할 필요가 없으며'main()'만으로도 충분합니다. – umutto

답변

1

, 당신은 input()에서 선택을 반환해야합니다

을 :

def menu(): 
    print ("1. Play lottery number generator.\n2. Play number analysis.\n3. Play rainfall calculator.\n4. Play total sales calculator.\n5. Exit/Quit.") 
    return input() 

그런 다음 당신은 아마 그 값을 테스트 한 후 한 번만 반복 당 선택 싶어하고

def main(): 
    while True: 
     choice = menu() 
     if choice == 1: 
      lottery_number_generator() 
     elif choice == 2: 
      number_analysis() 
     elif choice == 3: 
      rainfall() 
     elif choice == 4: 
      total_sales() 
     elif choice == 0: 
      quit 
     else: 
      print ("\n Not a valid choice try AGAIN.") 

#start of program 


print ("Welcome, Please select which HW assignment you would like to view:\nPlease select which HW assignment to play by pushing the number.\n") 

main() 

참고 : 파이썬 버전에 따라 테스트 할 때 "1" 또는 1 문자열을 처리 할 때주의해야 할 수도 있습니다. 값이 choice ...

+0

지금 제가 도와 주셔서 감사합니다. –

관련 문제