2016-12-17 109 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