Python版本:3.6.2 操作系统:Windows 作者:明镜应缺
1.求1-100的所有数的和
sum=0
start=1
while True:
sum=sum+start
if start==100:
break
start+=1
print(sum)
2.输出1-100内的所有奇数
jishu=0
while True:
if jishu%2==1:#注意:if之后的代码块,一定要缩进。这里的print和下面的break要缩进。
print(jishu)
jishu+=1
if jishu==100:
break
3.输出1-100内的所有偶数
oushu=0
while True:
if oushu%2==0:
print(oushu)
oushu+=1
if oushu==100:
break
4.求1-2+3-4+5.....99的所有数的和
sum=0
shu=0
while True:
if shu%2==1:
sum=sum+shu
elif shu%2==0:
sum=sum-shu
shu+=1
if shu==100:
break
print(sum)#这边的print,如果对标while,那么输出的就是循环完之后的和,如果对标if,那么就是每一步的和。由于加完99之后有break,所以输出不了加完99之后的数。
5.用户登录(三次重试机会)
i=0
while i<3:
user=input("请输入用户名:")
pwd=input("请输入密码:")
if user=="wangduo" and "pwd==qiuyu": #是==不是=,且wangduo和qiuyu要加引号表示字符串。
print("登录成功!")
break #这两个地方要缩进
else:
print("请重新输入")
i+=1
6.电脑,鼠标垫,键盘。通过让用户输入商品代码来选择商品。
# 电脑,鼠标垫,键盘。通过让用户输入商品代码来选择商品。
shangpin=["电脑","鼠标垫","键盘"]
for key,item in enumerate(shangpin):
print(key,item)
inp=input("请选择商品:")
# 这里的inp默认就是字符串,不是数字,后面需要用到数字形式的,所以需要转换成数字
print(shangpin[int(inp)])
7.元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
dic={"k1":[],"k2":[]}
i=[11,22,33,44,55,66,77,88,99,90]
for x in i:
if x<=66:
dic["k1"].append(x)
else:
dic["k2"].append(x)
print(dic)
8.查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in li:
b=i.strip()
if (b.startswith("a") or b.startswith("A")) and b.endswith("c"):
print(b)
9.输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
li = ["手机", "电脑", '鼠标垫', '游艇'] for x,y in enumerate(li,1): print(x,y) z=input("请输入商品序号:") if int(z)>0 and int(z)<=len(li): print(li[int(z)-1]) else: print("商品不存在")
10.功能要求:
- 要求用户输入总资产,例如:2000
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
goods
=
[
{
"name"
:
"电脑"
,
"price"
:
1999
},
{
"name"
:
"鼠标"
,
"price"
:
10
},
{
"name"
:
"游艇"
,
"price"
:
20
},
{
"name"
:
"美女"
,
"price"
:
998
},
]
#我写的,写的不好。后面有别人写的。 goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] car={} for x,y in enumerate(goods): print(x,y["name"],y["price"]) money=input("请输入总资产:") int_money=int(money) while True: select_goods=input("请选择商品(9结算):") if int(select_goods)==9: break else: good = int(select_goods) select_num=input("请选择商品数量:") num=int(select_num) name=goods[good]["name"] price=goods[good]["price"] car[name]={"数量":num,"价格":price*num} print(car) sum=0 for i in car: sum=sum+car[i]["价格"] if sum<=int_money: print("购买成功") else: print("穷逼") #别人写的 shopping_cart = [] salary = 2000 goods = [ {"name": "电脑", "price": 3000}, {"name": "鼠标", "price": 103}, {"name": "游艇", "price": 200000}, {"name": "美女", "price": 998}, ] for i in enumerate(goods): index = i[0] #序号 p_list = i[1] #商品清单 p_name_list = p_list.get('name')#商品名称列表 p_price_list = p_list.get('price')#商品价格列表 print(index,":",p_name_list,p_price_list) while True: choice = input("please enter your choice:").strip() if choice.isdigit():#如果选择为正整数 choice = int(choice)#输入为数字 if choice < len(goods) and choice >= 0:#选择小于列表长度大于0时 p_item = goods[choice]#加入购物车 p_name = p_item.get('name') p_monery = p_item.get('price') if p_monery <= salary:#如果商品价格小于等于余额 shopping_cart.append(p_item)#加入购物车 salary -= p_monery#结算 print("购买的商品 33[32m:%s 33[0m已加入到购物车".center(40, '-') %p_name) for p_item in shopping_cart: print(p_name,p_monery) print("您的余额为 33[31m:%s 33[0m元" % salary) else: print("您的余额不足,差%s元"%(abs(p_monery - salary))) else: print("没有此件商品!") else: print("参数错误") if choice == "q" or choice == "quit": cost = 0 print("您购买的商品清单如下:") for p in shopping_cart: print(p_name, p_monery) cost += p_monery print("