python - 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
和所有在同一个中。因此,这会计算正确还是只会产生另一个错误?dPrice5
dResortfee1
dResortfee2
def
我在 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” 无论如何,对于这个快速问题感到抱歉,但这些是我遇到的基本问题。
解决方案
您需要调用函数并分配这些变量,然后才能在语句中查询它们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')
推荐阅读
- amazon-web-services - IAM 角色未显示在修改 IAM 角色页面的 aws 控制台中
- vba - 宏在 excel 2010 中无法按预期工作
- c# - 我可以将 Azure.Core 命名空间用于非天蓝色项目吗?
- python - 从 2 个字符串列表中获取一个列表
- c# - 有什么方法可以在 c# winform in-project SQL Server 中插入中文吗?
- c++ - 使用函数指针为模板类指定推导指南
- python - 寻找 python 脚本将文件夹移动到具有相似名称的文件夹中
- python - 如何在 manim 中播放具有交错开始时间的动画
- python - 列表中的 Altair 注释
- javascript - 使用 JS 的 div 切换未按预期工作