この記事では主に Python でのショッピング カート プログラムの実装について詳しく紹介します。興味のある方は参考にしてください。
この記事の例では、Python ショッピング カート プログラム、具体的な内容を共有します。は次のとおりです
要件:
プログラムを開始した後、ユーザーに給与を入力させ、製品リストを印刷させます
ユーザーが製品番号に基づいて製品を購入できるようにします
商品を選択すると、残高が十分であるかどうかが確認され、残高が不足している場合は、通知が表示されます
終了するときは、いつでも終了できます。購入した商品と残高
残高が不足している場合は、チャージすることができます
コード:
#coding=utf-8 #Version:python 3.6.0 #Tools:Pycharm 2017.3.2 _date_ = '2018/4/16/016 14:50' _author_ = 'Hongyong' salary = int(input("Please input your salary: ")) shoppingmart = [] items = (["1","Huawei","¥",2800], ["2","Earphone","¥",300], ["3","Book","¥",80]) msg_items = ''' ----------items---------- 1. Huawei ¥ 2800 2. Earphone ¥ 300 3. Book ¥ 80 ------------------------- ''' print(msg_items) while True: shopindex = int(input("Please choose goods: ")) if salary > items[shopindex-1][3]: shoppingmart.append(items[shopindex-1]) salary -= int(items[shopindex-1][3]) print("You have bought {name} !".format(name = items[shopindex-1][1])) print("Your balance is: ¥",salary) decision = input("Do you want to quit now?") print(msg_items) else: print("Your balance is not enough! Please try sth else.") recharge_ans = input("Do you want to recharge?") if recharge_ans == "y": recharge = int(input("Please input money: ")) print("Please wait for a while...") salary += recharge print("You have recharged successfully!") print("And the balance is: ",salary,"now!") decision = input("Do you want to quit now?") print(msg_items) if decision == "q": break else: continue print("You have bought: ",shoppingmart) print("Your balance is: ¥",salary) print("Welcome your next coming!")
プログラム効果:
Please input your salary: 0 ----------items---------- 1. Huawei ¥ 2800 2. Earphone ¥ 300 3. Book ¥ 80 ------------------------- Please choose goods: 1 Your balance is not enough! Please try sth else. Do you want to recharge?y Please input money: 30000 Please wait for a while... You have recharged successfully! And the balance is: 30000 now! Do you want to quit now? ----------items---------- 1. Huawei ¥ 2800 2. Earphone ¥ 300 3. Book ¥ 80 ------------------------- Please choose goods: 1 You have bought Huawei ! Your balance is: ¥ 27200 Do you want to quit now? ----------items---------- 1. Huawei ¥ 2800 2. Earphone ¥ 300 3. Book ¥ 80 ------------------------- Please choose goods: 2 You have bought Earphone ! Your balance is: ¥ 26900 Do you want to quit now?q ----------items---------- 1. Huawei ¥ 2800 2. Earphone ¥ 300 3. Book ¥ 80 ------------------------- You have bought: [['1', 'Huawei', '¥', 2800], ['2', 'Earphone', '¥', 300]] Your balance is: ¥ 26900 Welcome your next coming!
関連するおすすめ:
以上がPythonでショッピングカートプログラムを実装の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

inpython、youappendelementStoalistusingtheappend()method.1)useappend()forsingleelements:my_list.append(4).2)useextend()or = formultipleElements:my_list.extend(another_list)ormy_list = [4,5,6] .3)forspecificpositions:my_list.insert(1,5).beaware

シェバンの問題をデバッグする方法には次のものがあります。1。シバン行をチェックして、それがスクリプトの最初の行であり、接頭辞スペースがないことを確認します。 2.通訳パスが正しいかどうかを確認します。 3.通訳を直接呼び出してスクリプトを実行して、シェバンの問題を分離します。 4. StraceまたはTrustsを使用して、システムコールを追跡します。 5.シバンに対する環境変数の影響を確認してください。

pythonlistscanbemanipulatedsingseveralmethodstoremoveElements:1)theremove()methodremovesthefirstoccurrenceofaspecifiedValue.2)thepop()methop()methodremovessanelementatagivenindex.3)thedelstatementementementementementementementementementemoritemoricedex.4)

Integers、strings、floats、booleans、otherlists、anddictionaryを含むpythonlistscanstoreanydatype

PythonListsSupportNumersoperations:1)AddingElementSwithAppend()、Extend()、Andinert()

Numpyを使用して多次元配列を作成すると、次の手順を通じて実現できます。1)numpy.array()関数を使用して、np.array([[1,2,3]、[4,5,6]])などの配列を作成して2D配列を作成します。 2)np.zeros()、np.ones()、np.random.random()およびその他の関数を使用して、特定の値で満たされた配列を作成します。 3)アレイの形状とサイズの特性を理解して、サブアレイの長さが一貫していることを確認し、エラーを回避します。 4)np.reshape()関数を使用して、配列の形状を変更します。 5)コードが明確で効率的であることを確認するために、メモリの使用に注意してください。

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

Forpythondatastorage、chooseLists forfficability withmixeddatypes、array.arrayformemory-efficienthogeneousnumericaldata、およびnumpyArrays foradvancednumericalcomputing.listSareversatilebuteficient efficient forlargeNumericaldatates;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ホットトピック









