首页 > 解决方案 > def 的 Python 问题,def 中的 var 错误,if 语句中的变量不起作用

问题描述

我用def roomidea(test)里面的输入做了一个,我相信我做对了return。现在在我的第一if条语句下,sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea(test)它读取了函数。问题是当我运行它显示的代码时

sBedsize没有定义

即使我在if声明中拥有所有这些。为什么这不起作用?

我已经在这个项目上工作了一段时间,现在我认为我的第二个def price(test2)工作但我还不确定,因为第一个def是迄今为止的主要问题,我将不胜感激任何提示或帮助或任何可以帮助我解决或学习的东西如何解决这个问题。

现在我还有一个很大的问题是我在第二个中使用了许多变量def,这意味着我使用变量来解决其他变量,比如和dTotalqav有、、、dPrice1和所有在同一个中。因此,这会计算正确还是只会产生另一个错误?dPrice5dResortfee1dResortfee2def

我在 PyCharm 中运行它,以防万一。

import time
def roomidea(test):
    sBedsize = input("For your room do you want a queen-size bed or a king-size bed? Type Queen for queen-size or King for king-size:\t")
    sRoomview = input("For your room do you want the standard view or the atrium view? Type Standard for standard-view or Atrium for atrium-view:\t")
    sVehicle = input("Will you be parking at our hotel? Type Yes if you are, or No if you are not:\t")
    iRoomnum = int(input("Please enter the amount of rooms you wish to check out:\t"))
    iNights = int(input("Please enter the amount of nights you wish to stay:\t"))
    return sBedsize, sRoomview, sVehicle, iRoomnum, iNights

def price(test2):
    dPrice1 = iNights * 280
    dPrice2 = iNights * 320
    dPrice3 = iNights * 295.50
    dPrice4 = iNights * 335.50
    dPrice5 = iNights * 15.75
    dResortfee1 = iNights * 20
    dResortfee2 = iRoomnum * 20
    dTotalqs = dPrice1 + dPrice5 + dResortfee1 + dResortfee2
    dTotalqa = dPrice2 + dPrice5 + dResortfee1 + dResortfee2
    dTotalqsv = dPrice1 + dResortfee1 + dResortfee2
    dTotalqav = dPrice2 + dResortfee1 + dResortfee2
    dTotalks = dPrice3 + dPrice5 + dResortfee1 + dResortfee2
    dTotalka = dPrice4 + dPrice5 + dResortfee1 + dResortfee2
    dTotalksv = dPrice3 + dResortfee1 + dResortfee2
    dTotalkav = dPrice4 + dResortfee1 + dResortfee2
    dTaxqs = dTotalqs * 0.15
    dTaxqa = dTotalqa * 0.15
    dTaxqsv = dTotalqsv * 0.15
    dTaxqav = dTotalqav * 0.15
    dTaxks = dTotalks * 0.15
    dTaxka = dTotalka * 0.15
    dTaxksv = dTotalksv * 0.15
    dTaxkav = dTotalkav * 0.15
    dOverallqs = dTotalqs + dTaxqs
    dOverallqa = dTotalqa + dTaxqs
    dOverallqsv = dTotalqsv + dTaxqs
    dOverallqav = dTotalqav + dTaxqs
    dOverallks = dTotalks + dTaxqs
    dOverallka = dTotalka + dTaxqs
    dOverallksv = dTotalksv + dTaxqs
    dOverallkav = dTotalkav + dTaxqs
    return dPrice1, dPrice2, dPrice3, dPrice4, dPrice5, dResortfee1, dResortfee2, dTotalqs, dTotalqa, dTotalqsv, dTotalqav, dTotalks, dTotalka, dTotalksv, dTotalkav, dTaxqs, dTaxqa, dTaxqsv, dTaxqav, dTaxks, dTaxka, dTaxksv, dTaxkav, dOverallqs, dOverallqa, dOverallqsv, dOverallqav, dOverallks, dOverallka, dOverallksv, dOverallkav



if  sBedsize == "Queen" and sRoomview == "Standard" and sVehicle == "Yes":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea(test)
    print("The price for a Queen-Size bed with the Standard view and you are parking a vehicle, this comes to the total of $%.2f" % dTotalqs + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "Queen" and sRoomview == "Atrium" and sVehicle == "Yes":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price of Queen-Size bed with the Atrium view and you are parking a vehicle, this comes to the total of $%.2f" % dTotalqa + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "Queen" and sRoomview == "Standard" and sVehicle == "No":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a Queen-Size bed with the Standard view and you are not parking a vehicle, this comes to the total of $%.2f" % dTotalqsv + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "Queen" and sRoomview == "Atrium" and sVehicle == "No":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a Queen-Size bed with the Atrium view and you are not parking a vehicle, this comes to the total of $%.2f" % dTotalqav + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "King" and sRoomview == "Standard" and sVehicle == "Yes":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a King-Size bed with the Standard view and you are parking a vehicle, this comes to the total of $%.2f" % dTotalks + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "King" and sRoomview == "Atrium" and sVehicle == "Yes":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a King-Size bed with the Atrium view and you are parking a vehicle, this comes to the total of $%.2f" % dTotalka + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "King" and sRoomview == "Standard" and sVehicle == "No":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a King-Size bed with the Standard view and you are not parking a vehicle, this comes to the total of $%.2f" % dTotalksv + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
elif sBedsize == "King" and sRoomview == "Atrium" and sVehicle == "No":
    sBedsize, sRoomview, sVehicle, iRoomnum, iNights = roomidea()
    print("The price for a King-Size bed with the Standard view and you are not parking a vehicle, this comes to the total of $%.2f" % dTotalkav + " If this is how you want your room, type Yes, if you want to pick another option, type No.")
else:
    print("You did not enter the information correctly")
time.sleep(5)
feedback = input("Would you like to change your room choices? Type Yes or No for the options").upper()
if feedback == "YES":
    print("Thank you for picking a room with us, I hope you enjoy your room and we are hoping to see you back again!")
elif feedback == "NO":
    print("We are sorry that the room you picked isn't how you wanted it, please retype how you want your room, if you want to leave just type quit")
    time.sleep(5)
    print(test)

最后是导入时间,然后是 time.sleep(5),然后是 print(test),我正在使用它,所以它重新打印原始输入,以便他们可以更改输入,也许如果他们选择“Queen Standard Yes 1 1" 但他们犯了一个错误,他们可以输入 no 并将输入更改为“King Standard Yes 1 1” 无论如何,对于这个快速问题感到抱歉,但这些是我遇到的基本问题。

标签: pythonfunction

解决方案


您需要调用函数并分配这些变量,然后才能在语句中查询它们if

这是一个简化的示例:

def foo():
    # These variables only exist inside the function
    one = 1
    two = 2
    three = 3

    # This doesn't cause them to exist outside of the function; it "gives them"
    # as a result of calling the function later
    return one, two, three

# a, b, and c don't exist yet so I can't use them in an if statement here

# This is where we run the code contained in the function foo and bind the
# returned values to variables (they can be named differently, here we use a,
# b, and c)
a, b, c = foo()

# a, b, and c now exist as variables in the main program and they hold the values
# that were returned from the function. Now we can use them in an `if` statement, e.g.
if a or b or c:
    print('Success')

推荐阅读