返回值的错误

时间:2016-12-17 19:43:09

标签: python return

更新:所以在接受一些建议之后,我已经改变了返回值的方式。编辑内容如下所示。但是,该程序现在告诉我displayRent()函数缺少' deposit'价值,即使我现在正确地返回它。有什么想法吗?

所以我为编程决赛编写了这个程序。它根据公寓的类型和(值1-3,退出)和他们选择的家具或无家具进行用户输入。使用这些值,它可以找到名称,存款和租金。但是,出于某种原因,我的值不会返回到main()函数中,而下一个函数需要它们。

旁注:编写此代码的方式符合我的教授的指示。 这个程序也没有完成。但是这些代码给我带来的问题阻碍了我的进步。

感谢所有帮助!

##################################################
# 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()

2 个答案:

答案 0 :(得分:0)

<form action="" method="post">{% csrf_token %} {{ form.non_field_errors }} <div class="fieldWrapper"> {{ form.todo_name.errors }} <label for="{{ form.name.id_for_label }}">Name:</label> {{ form.todo_name }} </div> <div class="fieldWrapper"> {{ form.todo_description.errors }} <label for="{{ form.todo_description.id_for_label }}">Description</label> {{ form.todo_description }} </div> <input type="submit" value="Update" /> </form> 正在返回值,是吗?所以,抓住他们。

getType()

第二个问题:choiceInput, furnishedInput = getType() # Get the values determineRent(choiceInput, furnishedInput) # Same issue here... 返回一个字符串。你必须施展它

input()

并提示:通过降低值

可以简化if语句
choiceInput = int(input("Choice: "))

答案 1 :(得分: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):

另外,考虑使用循环,if条件条件只会捕获一个错误输入,如果用户输入2个错误条目怎么办?