检查int是否在数据库表中。(收银机)

2024-09-29 00:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的一个班级里创建了一个收银机,我们的老师希望我们输入万国邮政编码(UPC)。到目前为止,我已经创建了表并插入了所有产品。虽然有二十个,所以我这里只放了三个。你知道吗

我遇到的问题是如何检查用户的输入,看它是否与数据库中的UPC(即000100020003)匹配。长话短说,如何检查字符串是否与数据库表中的值匹配。你知道吗

如果你想看到整个编码得到一个更好的想法,向下滚动更好

import sqlite3

c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS tblStoreItems(intUniversalProductCode 
INT,strProductName TEXT,intQuantity INT,fltPrice REAL)')
conn.commit()

def funStoreItems():
    c.execute("INSERT INTO 
tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) 
VALUES(0001,'EGGS',421,3.5)")#1,473.5

    c.execute("INSERT INTO 
tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) 
VALUES(0002,'TURKEY',803,7.75)") #6,223.25

    c.execute("INSERT INTO 
tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) 
VALUES(0003,'HAM',921,7.25)") #6,720.75

#funStoreItems()
UPC = int(input("Please enter the UPC:"))

完整代码:

import sqlite3

print("Opened database successfully!")

c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS tblStoreItems(intUniversalProductCode INT,strProductName TEXT,intQuantity INT,fltPrice REAL)')
conn.commit()

def funStoreItems():
    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0001,'EGGS',421,3.5)")#1,473.5

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0002,'TURKEY',803,7.75)") #6,223.25

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0003,'HAM',921,7.25)") #6,720.75

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0004,'BREAD',212,4)")  #848

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0005,'PANCAKE MIX',104,8)") #832

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0006,'CHEESE',742,2)") #1,484

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0007,'SAUSAGE',654,10)") #6,540

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0008,'CEREAL',1000,2)") #2,000

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0009,'MILK',1223,4.5)") #55053.5

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0010,'ORANGE JUICE',542,4)") #2,096

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0011,'SALAD',213,3)") #639

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0012,'ICE CREAM',666,5)") #3,330

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0013,'CHOCOLATE',1268,1)") #1,268

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0014,'CORN',364,3.75)") #1,365

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0015,'PASTA',196,6.75)")#1,323

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0016,'BANANA',150,4)") #600

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0017,'APPLE',407,4)") #1,628

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0018,'STEAK',545,15)") #8,175

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0019,'CARROT',312,2.5)") #802.5

    c.execute("INSERT INTO tblStoreItems(intUniversalProductCode,strProductName,intQuantity,fltPrice) VALUES(0020,'SUSHI',53,20)") #1,060

    conn.commit()


def funBankAccount():
  #103461.50
  total_balance = float(103461.50)

  wallet = print("\nYour current balance is:",total_balance)

  withdrawal = float(input("\nHow much money would you like to draw from the bank?"))


  if withdrawal > total_balance:
    overload = input("\nYou do not have enough to withdraw! would you just like to withdraw all of your money?").upper()

    if overload == 'yes'.upper():
      wallet = total_balance
      print("You now have", wallet,"in your wallet.")

    elif overload == 'no'.upper:
      control_center()

  #If the withdrawal amount is less than total balance
  elif withdrawal < total_balance:
    #Take away how much the user has inputted
    total_balance = (total_balance - withdrawal)
    #Withdraw the amount of money that the user withdrawed.
    wallet = withdrawal
    print("\nYou now have",wallet,"in your wallet")
  else:
    print("Invalid! Please try again")
    funBankAccount()

def funCashRegister():
  print("\n________________________________________________\nHere are the list of things that you can buy:\n==================================\nItem: Eggs         |  UPC: 0001  \nItem: Turkey       |  UPC: 0002  \nItem: Ham          |  UPC: 0003  \nItem: Bread        |  UPC: 0004  \nItem: Pancake      |  UPC: 0005  \nItem: Cheese       |  UPC: 0006  \nItem: Sausage      |  UPC: 0007  \nItem: Cereal       |  UPC: 0008  \nItem: Milk         |  UPC: 0009  \nItem: Orange Juice |  UPC: 0010  \nItem: Salad        |  UPC: 0011  \nItem: Ice Cream    |  UPC: 0012  \nItem: Chocolate    |  UPC: 0013  \nItem: Corn         |  UPC: 0014  \nItem: Pasta        |  UPC: 0015  \nItem: Banana       |  UPC: 0016  \nItem: Apple        |  UPC: 0017  \nItem: Steak        |  UPC: 0018  \nItem: Carrot       |  UPC: 0019  \nItem: Sushi        |  UPC: 0020  \n==================================")

  #STEP 1)This will ask the user for the input of the UPC
  #user_input_upc = int(input("\nPlease enter the Universal Postal Code here for the produc tthat you are wanting to purchase today."))
  UPC_or_Item = ""
  UPC = int(input("Please enter the UPC:"))

  #take out the numbers from the string and convert them into digits

  #Otherwise if they don't type in digits then continue.



  #Step 2)  Retreiving Product description and price(database)
  #Grab the item of the UPC then print it out telling the user how much they have to pay for it
  #aaaa = float(input("How much did the customer pay you"))

  #Search how to check if a string matches value in a table in a database in python

  #OR check how I did that inside the password database.

  #print("You have to pay", x,"before acquiring this.")


  #Step 3)Give the cashier a way to signal that the transaction is complete.  Afterwards, provide subtotal, tax, total cash tendered and calculate changes


  #Step3.1) USE OWASSO TAX: 8.917%.  Do the math

  #Step3.2) Remove quantity and once it hits zero, tell the user that they can no longer purchase the item for it is out of stock.

  #Step4) Print out the change and confirmation of purchase.


def control_center():

  #funBankAccount()

  #funStoreItems()

  funCashRegister()

control_center()

Tags: theinputexecutetotalinsertvaluesbalanceupc
1条回答
网友
1楼 · 发布于 2024-09-29 00:18:25

您可以使用SQL本身来实现它。条件对你有帮助的地方。你知道吗

if c.execute("select * from tblStoreItems where intUniversalProductCode="+str(upc)) is None:
     #When there is no entry for the upc, the query hits the DB and brings nothing. so, this if condition gets executed.
     print("Sorry, we dont have such an entry!!")
else:
     #when the query returns an output it means, there is an entry for the upc
     for item in c.execute("select * from tblStoreItems where intUniversalProductCode="+str(upc)):
             print(item)

输出:

(1,“蛋”,421,3.5)

相关问题 更多 >