<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>运达&#039;s  blog &#187; Python</title>
	<atom:link href="https://www.yunda51.com/?cat=163&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>Python用户登录及简单的购物车功能</title>
		<link>https://www.yunda51.com/?p=1867</link>
		<comments>https://www.yunda51.com/?p=1867#comments</comments>
		<pubDate>Thu, 21 Dec 2017 03:16:51 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[登陆]]></category>
		<category><![CDATA[购物车]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1867</guid>
		<description><![CDATA[最近研究Python写写简单的小功能（注：代码格式一定要注意行缩进）： #!/usr/bin/python #<a href="https://www.yunda51.com/?p=1867" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>最近研究Python写写简单的小功能（注：代码格式一定要注意行缩进）：</p>
<pre class="wp-code-highlight prettyprint">
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#简单的用户登陆功能（限制5次）
import getpass
i=0
while i&lt;5:
    name = input(&quot;请输入用户名：&quot;)
    pwd  = getpass.getpass(&#039;请输入密码：&#039;)
    if name==&quot;ceshi&quot; and pwd==&quot;123456&quot;:
        print(&quot;欢迎普通用户登录成功！&quot;)
        break
    elif name==&quot;liuyd&quot; and pwd==&quot;123456&quot;:
        print(&quot;欢迎管理员登录成功！&quot;)
        break
    else:
        print(&quot;用户名或密码输入错误！&quot;)

</pre>
<hr />
<pre class="wp-code-highlight prettyprint">
#功能要求：
#!/usr/bin/env python
#输出商品列表，用户输入序号，显示用户选中的商品
# 商品 goods_list = [&quot;iPhoneX&quot;, &quot;机械键盘&quot;, &#039;联想电脑&#039;, &#039;电脑桌&#039;]
# 要求用户输入总资产，例如：8000
# 显示商品列表，让用户根据序号选择商品，加入购物车
# 购买，如果商品总额大于总资产，提示账户余额不足，否则，购买成功。
goods_list = [{&quot;name&quot;: &quot;iPhoneX&quot;, &quot;price&quot;: 8888},
              {&quot;name&quot;: &quot;机械键盘&quot;, &quot;price&quot;: 1200},
              {&quot;name&quot;: &quot;联想电脑&quot;, &quot;price&quot;: 4000},
              {&quot;name&quot;: &quot;电脑桌&quot;, &quot;price&quot;: 250}]
shopping_list = []
i = 0
for key,values in goods_list:
    print(i+1,goods_list[i][&#039;name&#039;],goods_list[i][&#039;price&#039;])
    i+=1
total_assets = input(&quot;请输入你的资产&quot;)
total_money = int(total_assets)
while True:
    input_val = input(&quot;请输入购买商品的序号,结束购买请按0，查看购物车请按9&quot;)
    if int(input_val) == 1:
        shopping_list.append(goods_list[int(input_val) - 1])
    elif int(input_val) == 2:
        shopping_list.append(goods_list[int(input_val) - 1])
    elif int(input_val) == 3:
        shopping_list.append(goods_list[int(input_val) - 1])
    elif int(input_val) == 4:
        shopping_list.append(goods_list[int(input_val) - 1])
    elif int(input_val) == 9:
        #查看购物车所有商品
        # for key,value in shopping_list.items():
        #     print(key,value)
        print(shopping_list)
        while True:
            input_val = input(&quot;请输入要移除商品的序号,结束请按0,查看购物车剩余商品请按8&quot;)
            if int(input_val)==1:
                shopping_list.remove(goods_list[int(input_val) -1])
                print(shopping_list)
            elif int(input_val)==2:
                shopping_list.remove(goods_list[int(input_val) -1])
                print(shopping_list)
            elif int(input_val)==3:
                shopping_list.remove(goods_list[int(input_val) -1])
                print(shopping_list)
            elif int(input_val)==4:
                shopping_list.remove(goods_list[int(input_val) -1])
                print(shopping_list)
            elif int(input_val)==8:
                print(shopping_list)
            elif int(input_val) == 0:
                break
    elif int(input_val) == 0:
        break
    else:
        print(&quot;请输入正确的商品序号！&quot;)
#打印结束之后查看剩余的商品
print(shopping_list)
sum = 0
total = 0
for i in shopping_list:
    #商品价格
    sum = int(i[&#039;price&#039;])
    #总金额数
    total += sum
print(&quot;购物总金额：&quot;, total)
while True:
    if total_money &gt;= total:
        input_val = input(&quot;请按1确认购买&quot;)
        if int(input_val)==1:
            #剩余的资产数（总资产-商品总价格）
            total_money = total_money-total
            print(&quot;购买成功，您的资产剩余：&quot;,total_money)
            break
    else:
        input_val = input(&quot;您的资产不足，无法购买！请按1进行充值，其他请退出&quot;)
        if int(input_val)==1:
            input_val = input(&quot;请输入充值金额&quot;)
            #总资产（剩余的资产+充值的资产）
            total_money = total_money + int(input_val)
            print(&quot;充值之后的总资产为&quot;,total_money)
            continue
        else:
            print(&quot;取消购买！&quot;)
            break
</pre>
<p>转载请注明转自:<a href="http://www.yunda51.com">运达's blog</a> 原文地址：<a href="http://www.yunda51.com/1867.html">http://www.yunda51.com/1867.html</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1867</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
