2016-12-17 68 views
0

更新:所以在採取了一些建議之後,我改變了返回值的方式。編輯如下所示。但是,該程序現在告訴我displayRent()函數缺少'存款'值,即使我現在正確地返回它。有任何想法嗎?返回值的錯誤

所以我正在爲我的編程最終編寫這個程序。它根據公寓的類型和用戶輸入(值1-3,4退出)以及他們選擇裝修還是未裝修。通過這些值,它可以找到名稱,存款和租金。但是,出於某種原因,我的值沒有被返回到main()函數中,鏈接中的下一個函數需要它們。

Sidenotes:這段代碼的寫法與我的教授一致。 該程序還未完成。但是,這些代碼給我的問題阻礙了我的進步。

所有幫助表示讚賞!

################################################## 
# This program displays possible living spaces, # 
# and gives total prices upon certain options of # 
# a said space is chosen.      # 
################################################## 

############################### 
# Matthew Bobrowski   # 
# CSC122-07 Final    # 
# December 17th, 2016   # 
############################### 

print("""Matthew Bobrowski 
CSC 122-07 Final Program 
December 18th, 2016, 11:59pm""") 

def main(): 

    print("Please choose one of the options listed. (1-4)") 
    print(""" 
    1. Studio 
    2. One-Bedroom 
    3. Two-Bedroom 
    4. Exit 
    """) 
    choiceInput, furnishedInput = getType() 
    rent, deposit = determineRent(choiceInput, furnishedInput) 
    displayRent(choiceInput, rent, deposit) 

def getType(): 
    choiceInput = input("Choice: ") 
    furnishedInput = input("Furnished? (Y/N): ") 
    if choiceInput != 1 or choiceInput != 2 or choiceInput != 3 or choiceInput != 4: 
     print("Invalid entry. Please try again.") 
     choiceInput = input("Choice: ") 
    if furnishedInput != 'Y' or furnishedInput != 'y' or furnishedInput != 'N' or furnishedInput != 'n': 
     print("Invalid entry. Please try again.") 
     furnishedInput = input("Furnished? (Y/N): ") 
    return choiceInput, furnishedInput 

def determineRent(choiceInput, furnishedInput): 
    rent = 0 
    deposit = 0 

    if choiceInput == 1: 
     if furnishedInput == 'Y' or furnishedInput == 'y': 
      rent = 750 
      deposit = 400 
     elif furnishedInput == 'N' or furnishedInput == 'n': 
      rent = 600 
      deposit = 400 
    elif choiceInput == 2: 
     if furnishedInput == 'Y' or furnishedInput == 'y': 
      rent = 900 
      deposit = 500 
     elif furnishedInput == 'N' or furnishedInput == 'n': 
      rent = 750 
      deposit = 500 
    elif choiceInput == 3: 
     if furnishedInput == 'Y' or furnishedInput == 'y': 
      rent = 1025 
      deposit = 600 
     elif furnishedInput == 'N' or furnishedInput == 'n': 
      rent = 925 
      deposit = 600 
    elif choiceInput == 4: 
     quit 
    return rent, deposit 

def displayRent(choiceInput, furnishedInput, rent, deposit): 
    if choiceInput == 1: 
     if furnishedInput == 'y' or furnishedInput == 'Y': 
      print(""" 
      TYPE: STUDIO - FURNISHED 
      DEPOSIT: $""" + str(deposit) + """ 
      RENT: $""" + str(rent)) 
     else: 
      print(""" 
      TYPE: STUDIO - UNFURNISHED 
      DEPOSIT: $""" + str(deposit) + """ 
      RENT: $""" + str(rent)) 
    return 


main() 

回答

0

getType()返回值,是嗎?所以,抓住他們。

choiceInput, furnishedInput = getType() # Get the values 
determineRent(choiceInput, furnishedInput) # Same issue here... 

問題二:input()返回一個字符串。你要投它

choiceInput = int(input("Choice: ")) 

和小費:你的if語句可以通過lowercasing值

if furnishedInput.lower() == 'y': 
+0

當我試圖做到這一點時,它每次回想兩次輸入,然後失敗。 – Matt

+0

大概你是比較字符串和整數 –

+0

其實,沒關係。之前的迭代有些不正確;它現在通過。非常感謝! – Matt

0

好了簡化,如果你有一個功能,您將返回的東西,你需要存儲返回結果:

def example(): 
    return "hello world" 
store = example() 
print example 

如果您想比較是否選擇了您的選項,請使用and

if choiceInput != 1 and choiceInput != 2 and choiceInput != 3 and choiceInput != 4: 

或稍微巨蟒方式:

if choiceInput not in (1,2,3,4): 

也,考慮使用一個循環,如果條件情況只會趕上一個錯誤的輸入,如果有什麼用戶輸入2故障項?

+0

忘記添加我的程序檢查無效輸入的事實,但在調試過程中將其刪除。我讓它重新輸入每個錯誤的輸入,直到給出一個可接受的輸入。 :) – Matt