2017-10-10 47 views
-5

這段代碼是爲了給這個人提供平均值,中位數,模式或退出的選項,但是我不能得到選項1-4來正確地工作,當他們選擇時會發生什麼該選項。有人可以幫我用這個代碼

ans1=ans1 
ans2=ans2 

# define functions 

def average(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    """This function adds two numbers""" 
    return total== num1, + num2, + num3, + num4, + num5, + num6, + num7, + num8, + num9, + num10 == ans1 
    total/ans1 

def median(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    """This function subtracts two numbers""" 
    return total == num1 - num2 - num3 - num4 - num5 - num6 - num7 - num8 - num9 - num10 == ans2 
    total/ans2 

def mode(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    """This function multiplies two numbers""" 
    return num1 * num2, num3, num4, num5, num6, num7, num8, num9, num10 

def quit(): 
    quit() 


# This gives the user the options in a menu format 
print("Select operation.") 
print("1.average") 
print("2.median") 
print("3.Mode") 
print("4.quit") 
#this is where the user enters the number for what the user wants done 
choice = input("Enter choice 1,2,3,4: ") 

################################################################# 
#this then asks the user for the 10 numbers 
num1 = int(input("Enter first number: ")) 
num2 = int(input("Enter second number: ")) 
num3 = int(input("enter third number: ")) 
num4 = int(input("enter fourth number: ")) 
num5 = int(input("enter fith number: ")) 
num6 = int(input("enter sixth number: ")) 
num7 = int(input("enter seventh number: ")) 
num8 = int(input("enter eights number: ")) 
num9 = int(input("enter ninth number: ")) 
num10 = int(input("enter tenth number: ")) 

print(num1) 
print(num2) 
print(num3) 
print(num4) 
print(num5) 
print(num6) 
print(num7) 
print(num8) 
print(num9) 
print(num10) 



if choice == '1':   
    print (ans1, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10) 
def adding (num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    s = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/10 
    print("this is what they add up to") 
    print(s) 
    return s 

    elif choice == '2': 
     print(ans2(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)) 

    elif choice == '3': 
     print(num1,"*", num2, num3, num4, num5, num6, num7, num8, num9, num10,"=", multiply(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)) 
    elif choice == '4': 
     quit() 
+1

你應該首先清理代碼示例。 if-elif-block內有一個函數定義(帶有錯誤的縮進)。 –

+3

代碼中有很多錯誤。什麼'ans1 = ans1'和'ans2 = ans2'應該這樣做?那麼'num1 * num2,num3,num4,num5,num6,num7,num8,num9,num10'怎麼辦?它只會乘以兩個第一個元素並返回一個元組...並且錯誤繼續存在 –

+0

我們可以提供幫助,但這意味着我們必須從Python的基礎知識入手。有一個[教程](https://docs.python.org/3/tutorial/index.html)。 – Matthias

回答

2

因爲我不認爲這是家庭作業,但只是一個人努力學習一些Python我將部分地說明了如何可以做到這一點。

首先我們要在節目中很容易分開理解的功能,這是怎麼回事的基礎是showMenu()功能:

def showMenu(): 
    while True: 
    print("Select operation.") 
    print("1.average") 
    print("2.median") 
    print("3.Mode") 
    print("4.quit") 

    userInput = input() 

注意這個循環是條件始終True,因此不能逃脫。要添加一些邏輯,我們必須分析用戶輸入:

def showMenu(): 
    while True: 
    ... 
    userInput = input() 
    if userInput == "1" or userInput == "2" or userInput == "3": 
     # Do something 
    elif userInput == "4": 
     exit() 
    else: 
     print("Invalid input, try again") 

現在程序可以退出循環,如果用戶輸入4,如果他們進入其他任何無效的值,程序則只是再次顯示菜單,詢問用戶輸入一個值。現在,讓我們填寫部分的情況下,當輸入是1,2或3。我們編輯在if語句來分隔情況下,這些情況下,並創建一個新的函數來處理它們:

if userInput == "1": 
    showAverageMenu() 
elif userInput == "2": 
    showMedianMenu() 
elif userInput == "3": 
    showModeMenu() 
elif userInput == "4": 
    exit() 
else: 
    print("Invalid input, try again") 

好吧,讓我們下一步將是創建showAverageMenu()函數。但是,我們首先必須討論我們要處理用戶輸入的方式。你這樣做的方式是手動要求十個不同的輸入值,並將它們存儲在十個不同的變量名稱下。如果我們希望用戶輸入15個值,或30或100,該怎麼辦?這將需要大量的重複編碼。幸運的是,還有更好的方法:我們將把用戶輸入存儲在一個列表中。該列表可以與用戶想要的一樣大。因爲我們要求用戶輸入數字的方式在三個不同的操作中是相同的,所以我們將創建一個我們可以每次調用的函數。該功能將被稱爲getInput()。這個函數的邏輯將是這個樣子:

雖然沒有做過:

  1. 詢問用戶輸入一個數
  2. 檢查它是否是一個有效的輸入
  3. 追加有效輸入來一個清單

我們已經看到如何要求用戶輸入,但是,不像上次只有數字1到4有效,現在所有的數字都是有效的輸入。此外,上次我們將輸入作爲字符串處理,但爲了計算平均值,我們需要整數(或浮點)值。把一個字符串轉換成一個整數我們可以轉換成輸入:intValue = int(stringValue)。但是,如果用戶沒有輸入數字,但輸入了一些非數字字符,則此函數會提高 a ValueError。爲了處理這個我們必須捕獲的錯誤。

夠的話,讓我們展示一些代碼:

def getInput(): 
    inputList = [] 
    print("Please enter an integer") 
    while some_condition: 
    userInput = input() 
    try: 
     intValue = int(userInput) 
     inputList.append(intValue) 
    except ValueError: 
     print("'{}' is not an integer".format(userInput)) 

在這裏你可以看到,我們嘗試用戶輸入強制轉換爲整數,但如果失敗,則ValueError被捕獲,並且用戶及時提醒他們應該輸入一個數字。還要注意,這個函數從一個名爲inputList的空列表開始,如果轉換不失敗,該列表將被填充整數值。最後,注意while循環的條件,至今它被設置爲一個未定義的變量,但是期望的行爲是什麼?首先,輸入列表必須包含至少一個元素,其次,用戶必須發信號通知他們已完成輸入值。在這個代碼可以做這樣的:

def getInput(): 
    userInput = None 
    inputList = [] 
    print("Please enter an integer") 
    while userInput != "done" or len(inputList) == 0: 
    userInput = input() 
    ... 
     print("'{}' is not an integer".format(userInput)) 
    print("Please enter an integer") 
    if len(inputList) > 0: 
     print("Or type 'done' if you are finished") 

    return inputList 

現在我們開始通過設置userInputNone這樣我們就可以使用該變量的條件。如果輸入列表爲空,我們也繼續循環。最後,如果輸入列表包含數字,我們告訴用戶他們可以輸入'done'來退出循環,這反映在循環的條件中。如果用戶完成,我們返回列表。

現在我們已經創建了一個通用的解決方案來從用戶檢索輸入,我們必須定義實現這些操作的函數。讓我們來看看showAverageMenu()

def showAverageMenu(): 
    print("***Averaging***") 
    inputList = getInput() 
    avg = sum(inputList)/len(inputList)) 

    print("The Average of") 
    print(inputList) 
    print("is: {}\n".format(avg) 

正如你所看到的,該解決方案是很容易的。我們所要做的就是計算平均值並打印出來。要計算平均值,我們可以使用內置方法計算列表的總和並將其除以列表的長度。

現在其他兩種方法都有點困難,但作爲練習留給讀者;)

+0

嗨Jurgy我試過你的代碼,但我有一個錯誤在第15行 –

+0

這將有助於,如果你告訴我你的東西第15行是因爲我的答案中的代碼不是一個blob。此外,確切的錯誤將有助於知道。 – Jurgy

0

我爲您解決了幾個部分的代碼,並在解釋我做了什麼的註釋中。在這段代碼中有幾件事需要修復,而我沒有修復它們。我解釋了我做了什麼,所以你應該能夠通過並修復它們,但是如果你有任何問題請留言,我會盡我所能解釋。正如很多人上面所說的,你應該通過一個python教程。 Codecademy has a relatively good one.

#You should declare these first to ensure that you have access to them throughout the program 
num1 = int(input("Enter first number: ")) 
num2 = int(input("Enter second number: ")) 
num3 = int(input("enter third number: ")) 
num4 = int(input("enter fourth number: ")) 
num5 = int(input("enter fith number: ")) 
num6 = int(input("enter sixth number: ")) 
num7 = int(input("enter seventh number: ")) 
num8 = int(input("enter eights number: ")) 
num9 = int(input("enter ninth number: ")) 
num10 = int(input("enter tenth number: ")) 

print(num1) 
print(num2) 
print(num3) 
print(num4) 
print(num5) 
print(num6) 
print(num7) 
print(num8) 
print(num9) 
print(num10) 

#It looks like you are trying to use the variables above, so you don't need them as parameters. 
#If you use them as parameters, the function will get confused and use the wrong ones. 
def average(): 
    """This function adds two numbers""" 
    #Set total before you try to return it. 
    #When you create a variable, it's just 1 equal sign, it's 2 when you are checking if something is equal 
    #Also, never use 2 equal signs such as total = x == y 
    #When you are adding something, you don't need commas. 
    total = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 
    #The return statement should be the last thing in a function 
    return total 


def median(): 
    """This function subtracts two numbers""" 
    total = (num1 - num2 - num3 - num4 - num5 - num6 - num7 - num8 - num9 - num10)/2 
    return total 

#I'll let you take it from here. 
def mode(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    """This function multiplies two numbers""" 
    return num1 * num2, num3, num4, num5, num6, num7, num8, num9, num10 

def quit(): 
    quit() 


# This gives the user the options in a menu format 
print("Select operation.") 
print("1.average") 
print("2.median") 
print("3.Mode") 
print("4.quit") 
#this is where the user enters the number for what the user wants done 
choice = input("Enter choice 1,2,3,4: ") 


if choice == '1':   
    print (ans1, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10) 
def adding (num1, num2, num3, num4, num5, num6, num7, num8, num9, num10): 
    s = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/10 
    print("this is what they add up to") 
    print(s) 
    return s 

    #The first if statement should be an if statement, not an elif statement. Since this is a new function, it's a new if statement. 
    if choice == '2': 
     print(ans2(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)) 

    elif choice == '3': 
     print(num1,"*", num2, num3, num4, num5, num6, num7, num8, num9, num10,"=", multiply(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)) 
     elif choice == '4': 
     quit() 
+0

感謝您的幫助我只是想知道爲什麼它不給我一個平均的,因爲它只是打印數字 –

相關問題