Python 检查来自函数的类外输入?
我基本上创建了一个具有飞机和酒店登记功能的班级预订, 最后将用户名、姓氏、id插入mysql中的一个表中Python 检查来自函数的类外输入?,python,mysql,Python,Mysql,我基本上创建了一个具有飞机和酒店登记功能的班级预订, 最后将用户名、姓氏、id插入mysql中的一个表中 import mysql.connector as mysql class Reservation: 我的问题是如何在课堂外检查酒店检查输入是否为“是”或“否” 因为如果用户对入住酒店说“不”,我不希望他的名字、姓氏、id添加到mysql的表中,如果他说“是”,那么就“是” def __init__(self): self.passenger_id = int(
import mysql.connector as mysql
class Reservation:
我的问题是如何在课堂外检查酒店检查输入是否为“是”或“否”
因为如果用户对入住酒店说“不”,我不希望他的名字、姓氏、id添加到mysql的表中,如果他说“是”,那么就“是”
def __init__(self):
self.passenger_id = int(input("Put your id "))
self.passenger_fname = str(input("Your name "))
self.passenger_lname = str(input("Your last name "))
self.expenses = 0
self.airline_seats = {"Business Class":10,
"First Class":15,
"Economy":20}
self.airplane_prices = {"Business Class":1000,
"First Class":1500,
"Economy":2000}
self.hotel_room = {"Penthouse":2,
"Queen Bedroom":15,
"King Bedroom":20
}
self.hotel_prices = {"Penthouse":500,
"Queen Bedroom":250,
"King Bedroom":150}
def airplane_cost(self):
status = True
while(status):
try:
airplane_input= input("Choose: Business Class:1000,First Class:1500, Economy:2000 ")
for bed,value in self.airplane_prices.items():
if bed == airplane_input:
self.expenses += value
status = False
break
except Exception as e:
print(e)
def hotel_cost(self):
status = True
while(status):
try:
hotel_choice_input = input("You want to check in")
if hotel_choice_input == "yes" or hotel_choice_input=="Yes":
hotel_choice_input = input("Choose: Penthouse:500, Queen Bedroom:250,King Bedroom:150 ")
for room,value in self.hotel_prices.items():
if hotel_choice_input == room:
self.expenses += value
status = False
break
elif hotel_choice_input == "No" or hotel_choice_input == "no":
print("Okay have a nice day")
status = False
break
except Exception as e:
print(e)
def check_expenses(self):
print("Your cost is",self.expenses)
c = Reservation()
c.airplane_cost()
c.hotel_cost()
c.check_expenses()
db = mysql.connect(
host="localhost",
user = "root",
passwd="1234",
database="airplaneDb"
)
cursor = db.cursor(buffered=True)
# cursor.execute("CREATE TABLE Airplane1 (name VARCHAR(50),lastname VARCHAR(50), id int)")
# cursor.execute("CREATE TABLE Hotel1 (name VARCHAR(50),lastname VARCHAR(50), id int)")
sql = "INSERT INTO Airplane1 (name,lastname,id) VALUES (%s,%s,%s)"
sql1 = "INSERT INTO Hotel1 (name,lastname,id) VALUES (%s,%s,%s)"
val = (c.passenger_fname,c.passenger_lname,c.passenger_id)
cursor.execute(sql,val)
如果c.hotel\u check\u input='yes'谢谢你,那么你可以用
检查一下,我的脑子里有一艘常青的船,想不出任何东西,你不能分配一个self.passenger\u hotel\u choice
一旦你知道他们的选择,你就把它设置为true还是false?然后您可以简单地检索c.passenger\u hotel\u choice
。顺便说一句,使用捕获所有异常(异常除外)
是不好的做法,因为您不知道应该如何反应。您也不需要break
语句,因为status=False
已经让您脱离了循环
cursor.execute(sql1,val)
db.commit()
cursor.execute("SELECT * FROM Airplane1")
cursor.execute("SELECT * FROM Hotel1")