大家好,我想知道您是否可以幫助我解決有關 Python 資料庫 (SQLite3) 的問題。我想在我的程式中做一段時間。所以每次我成功輸入資料或完成作業后,我不想立即回傳主選單,而是想提出一個問題“你想繼續嗎?是/否”如果“是”那么它會去主選單,如果“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()
uj5u.com熱心網友回復:
find()將回傳子字串的索引,如果子字串不存在,則回傳 -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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376027.html
