鱼C论坛

 找回密码
 立即注册
查看: 1404|回复: 9

[已解决]请大神帮忙看下是否是局部变量的问题

[复制链接]
发表于 2019-10-15 21:49:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#商品列表
product_dist=[{'编号':0,'商品':'手机','价格':5000,'数量':20},
              {'编号':1,'商品':'手表','价格':50,'数量':20},
              {'编号':2,'商品':'自行车','价格':300,'数量':20}]
#购物车列表
shopping_car=[]
salary = input('请输入您的工资:')
#判断输入是否为数字
if salary.isnumeric() == True:
    salary = float(salary)
    while True:
#打印商品列表
        for header in ['编号', '商品', '价格', '数量']:
            print(header, end='\t')
        print('')
        for product_list in product_dist:
            print('%d\t\t%s\t%d\t\t%d' % (product_list['编号'],
                                          product_list['商品'],
                                          product_list['价格'],
                                          product_list['数量']))
#选择购买商品
        buy_product = int(input('请输入您需要购买的商品编号:'))
#判断商品
        if product_list['编号'] == buy_product:
            print(product_list['编号'])
            shopping_car.append(product_list['商品'])
            salary = salary - product_list['价格']
            print('您已经购买了%s,您的余额还有%d' % (product_list['商品'],
                                                         salary))
            product_list['数量'] = product_list['数量'] - 1
        else:
            print('您购买的商品不存在')
else:
    print('输入错误,请输入数字,谢谢!')




大神: if product_list['编号'] == buy_product:这句代码执行完后就直接执行else语句了!请问是因为 product_list是局部变量的问题吗!谢谢大神
最佳答案
2019-10-15 21:57:50
程序有点问题,帮你改了一下:

  1. #商品列表
  2. product_dist=[{'编号':0,'商品':'手机','价格':5000,'数量':20},
  3.               {'编号':1,'商品':'手表','价格':50,'数量':20},
  4.               {'编号':2,'商品':'自行车','价格':300,'数量':20}]
  5. #购物车列表
  6. shopping_car=[]
  7. salary = input('请输入您的工资:')
  8. #判断输入是否为数字
  9. if salary.isnumeric() == True:
  10.     salary = float(salary)
  11.     while True:
  12. #打印商品列表
  13.         for header in ['编号', '商品', '价格', '数量']:
  14.             print(header, end='\t')
  15.         print('')
  16.         for product_list in product_dist:
  17.             print('%d\t\t%s\t%d\t\t%d' % (product_list['编号'],
  18.                                           product_list['商品'],
  19.                                           product_list['价格'],
  20.                                           product_list['数量']))
  21. #选择购买商品
  22.         buy_product = int(input('请输入您需要购买的商品编号:'))
  23. #判断商品
  24.         if 0 <= buy_product <= 2:
  25.             print(buy_product)
  26.             shopping_car.append(product_dist[buy_product]['商品'])
  27.             salary = salary - product_dist[buy_product]['价格']
  28.             print('您已经购买了%s,您的余额还有%d' % (product_dist[buy_product]['商品'],
  29.                                                          salary))
  30.             product_dist[buy_product]['数量'] -= 1
  31.         else:
  32.             print('您购买的商品不存在')
  33. else:
  34.     print('输入错误,请输入数字,谢谢!')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-15 21:57:50 | 显示全部楼层    本楼为最佳答案   
程序有点问题,帮你改了一下:

  1. #商品列表
  2. product_dist=[{'编号':0,'商品':'手机','价格':5000,'数量':20},
  3.               {'编号':1,'商品':'手表','价格':50,'数量':20},
  4.               {'编号':2,'商品':'自行车','价格':300,'数量':20}]
  5. #购物车列表
  6. shopping_car=[]
  7. salary = input('请输入您的工资:')
  8. #判断输入是否为数字
  9. if salary.isnumeric() == True:
  10.     salary = float(salary)
  11.     while True:
  12. #打印商品列表
  13.         for header in ['编号', '商品', '价格', '数量']:
  14.             print(header, end='\t')
  15.         print('')
  16.         for product_list in product_dist:
  17.             print('%d\t\t%s\t%d\t\t%d' % (product_list['编号'],
  18.                                           product_list['商品'],
  19.                                           product_list['价格'],
  20.                                           product_list['数量']))
  21. #选择购买商品
  22.         buy_product = int(input('请输入您需要购买的商品编号:'))
  23. #判断商品
  24.         if 0 <= buy_product <= 2:
  25.             print(buy_product)
  26.             shopping_car.append(product_dist[buy_product]['商品'])
  27.             salary = salary - product_dist[buy_product]['价格']
  28.             print('您已经购买了%s,您的余额还有%d' % (product_dist[buy_product]['商品'],
  29.                                                          salary))
  30.             product_dist[buy_product]['数量'] -= 1
  31.         else:
  32.             print('您购买的商品不存在')
  33. else:
  34.     print('输入错误,请输入数字,谢谢!')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-15 22:04:56 | 显示全部楼层
zltzlt 发表于 2019-10-15 21:57
程序有点问题,帮你改了一下:

大神,请问为什么 if product_list['编号'] == buy_product:这句不会被执行!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-15 22:07:10 | 显示全部楼层
wwaswsz 发表于 2019-10-15 22:04
大神,请问为什么 if product_list['编号'] == buy_product:这句不会被执行!

product_list 为 {'编号':2,'商品':'自行车','价格':300,'数量':20},只有当编号为 2 时才会执行下面的代码。另外你的程序有点乱,一个 product_list,一个 product_dist,我都不明白你想干嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-15 22:12:42 | 显示全部楼层
zltzlt 发表于 2019-10-15 22:07
product_list 为 {'编号':2,'商品':'自行车','价格':300,'数量':20},只有当编号为 2 时才会执行下面的代 ...

被抢了......

PS:楼主,既然您的问题已解决,为什么还不设置最佳答案呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-15 22:13:25 | 显示全部楼层
zltzlt 发表于 2019-10-15 22:07
product_list 为 {'编号':2,'商品':'自行车','价格':300,'数量':20},只有当编号为 2 时才会执行下面的代 ...

大神,您好!
        product_list(商品表单)是遍历product_dist(商品列表)
        那么为什么product_list只是{'编号':2,'商品':'自行车','价格':300,'数量':20}呢而不是
             {'编号':0,'商品':'手机','价格':5000,'数量':20},
              {'编号':1,'商品':'手表','价格':50,'数量':20},
              {'编号':2,'商品':'自行车','价格':300,'数量':20}
              3个呢!
        谢谢大神指导!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-15 22:15:11 | 显示全部楼层
wwaswsz 发表于 2019-10-15 22:13
大神,您好!
        product_list(商品表单)是遍历product_dist(商品列表)
        那么为什么pr ...

product_list 是遍历 product_dist 遗留的数据,是 product_dist 的最后一个数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-15 22:15:51 | 显示全部楼层
zltzlt 发表于 2019-10-15 22:07
product_list 为 {'编号':2,'商品':'自行车','价格':300,'数量':20},只有当编号为 2 时才会执行下面的代 ...

大神,当我在 if product_list['编号'] == buy_product:前加上for product_list in product_dist:时程序输出正常,但是多了两个您购买的商品不存在。这应该是是for循环遍历的问题!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-15 22:17:11 | 显示全部楼层
zltzlt 发表于 2019-10-15 22:15
product_list 是遍历 product_dist 遗留的数据,是 product_dist 的最后一个数据。

谢谢大神,困扰两天了!谢谢您了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-15 22:19:04 | 显示全部楼层
zltzlt 发表于 2019-10-15 21:57
程序有点问题,帮你改了一下:

您的这种方法我尝试过,但忘了运行试下!shopping_car.append(product_dist[buy_product]['商品'])
尴尬!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-16 02:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表