Python: 数据库执行
Python: Database do while
大家好,我想知道您是否可以帮助我解决有关 Python 数据库 (SQLite3) 的问题。我想在我的程序中做一个 do-while。所以每次我成功输入数据或完成工作后,我不想立即返回主菜单,而是想问一个问题“你想继续吗?Y/N”如果“Y”那么它会转到主菜单,如果“N”它将终止程序。先谢谢你了!
import Databaseprofile
MENU_PROMPT = """--- Barangay Resident Record ---
Please choose one of these options:
[1] Add a new resident/profile.
[2] See all residents list.
[3] Exit.
Your selection: """
def menu():
connection = Databaseprofile.connect()
Databaseprofile.create_tables(connection)
while (user_input := input(MENU_PROMPT)) != "3":
if user_input == "1":
print("---Add a Resident---")
name = input("Enter full name: ")
birth = input("Enter birthdate (YY-MM-DD): ")
sex = input("Enter Sex: ")
purok = int(input("Enter your Purok: "))
house = int(input("Enter house number: "))
number = int(input("Enter mobile number: "))
Databaseprofile.add_profile(connection, name, birth, sex, purok, house, number)
print("---Added successfully!---")
elif user_input == "2":
print("--------------------------------------------------------------------------------------------------")
print("---Resident's List---")
profile = Databaseprofile.get_all_profile(connection)
for prof in profile:
print(f"{prof[0]}, {prof[1]}, {prof[2]}, {prof[3]}, Purok {prof[4]}, House Number {prof[5]}, Mobile Number {prof[6]}")
print("--------------------------------------------------------------------------------------------------")
#
else:
print("Invalid input, try again.")
print("Thank you for using our program.")
menu()
find()
将 return 子字符串的索引,如果子字符串不存在则为 -1。以下将循环直到标准输入收到“是”或“否”响应:
print("---Added successfully!---")
# loop until acceptable input is received
while True:
response = input("Do you want to continue? Y/N")[0]
if 'yYnN'.find(response) > -1:
break
# exit outer loop and terminate the program
if 'nN'.find(response) > -1:
break
大家好,我想知道您是否可以帮助我解决有关 Python 数据库 (SQLite3) 的问题。我想在我的程序中做一个 do-while。所以每次我成功输入数据或完成工作后,我不想立即返回主菜单,而是想问一个问题“你想继续吗?Y/N”如果“Y”那么它会转到主菜单,如果“N”它将终止程序。先谢谢你了!
import Databaseprofile
MENU_PROMPT = """--- Barangay Resident Record ---
Please choose one of these options:
[1] Add a new resident/profile.
[2] See all residents list.
[3] Exit.
Your selection: """
def menu():
connection = Databaseprofile.connect()
Databaseprofile.create_tables(connection)
while (user_input := input(MENU_PROMPT)) != "3":
if user_input == "1":
print("---Add a Resident---")
name = input("Enter full name: ")
birth = input("Enter birthdate (YY-MM-DD): ")
sex = input("Enter Sex: ")
purok = int(input("Enter your Purok: "))
house = int(input("Enter house number: "))
number = int(input("Enter mobile number: "))
Databaseprofile.add_profile(connection, name, birth, sex, purok, house, number)
print("---Added successfully!---")
elif user_input == "2":
print("--------------------------------------------------------------------------------------------------")
print("---Resident's List---")
profile = Databaseprofile.get_all_profile(connection)
for prof in profile:
print(f"{prof[0]}, {prof[1]}, {prof[2]}, {prof[3]}, Purok {prof[4]}, House Number {prof[5]}, Mobile Number {prof[6]}")
print("--------------------------------------------------------------------------------------------------")
#
else:
print("Invalid input, try again.")
print("Thank you for using our program.")
menu()
find()
将 return 子字符串的索引,如果子字符串不存在则为 -1。以下将循环直到标准输入收到“是”或“否”响应:
print("---Added successfully!---")
# loop until acceptable input is received
while True:
response = input("Do you want to continue? Y/N")[0]
if 'yYnN'.find(response) > -1:
break
# exit outer loop and terminate the program
if 'nN'.find(response) > -1:
break