如何让我的收据不再重复

2024-04-22

这是一个程序,要求用户输入条形码,然后我的程序找到指定的产品,询问用户他们想要购买多少产品,如果他们想继续,计算总价,然后假设打印出收据,但是我的问题是收据重复其值并且没有四舍五入到小数点后两位。

press 0 to stop shopping and print your reciept or press 1 to continue shopping0
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '2.80']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '0', '0']    

我很确定这就是代码混乱的地方(我的收据代码)

def quantity():
    fileOne = open('receipt.csv', 'a')
    writer = csv.writer(fileOne)
    global total_price
    product_data = read_csv_file()
    matches = search_user_input(product_data)
    if matches: # Will not be True if search_user_input returned None
        print("apple")
        product, price = matches[0], matches[1]
        order = int(input("How much of {} do you want?".format(product)))
        values = [str(product), str(price), str(order*price)]
        price = str(round(price,2))
        writer.writerows((values,))
        total_price.append(order * price)
    continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))
    if (continue_shopping == 0):
        fileOne.close()
        fileTwo = open("receipt.csv" , "r")
        reader = csv.reader(fileTwo)
        for row in reader:
            if row != None:
                print(row)
    elif continue_shopping==1:
        quantity()

这是作为一个实体的整个代码

import csv 
import locale
continue_shopping = 0
total_price = []

locale.setlocale( locale.LC_ALL, '' )
def read_csv_file():
    global total_price
    """ reads csv data and appends each row to list """
    csv_data = []
    with open("task2.csv") as csvfile:
         spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
         for row in spamreader:
             csv_data.append(row)
    return csv_data


def get_user_input():
    global total_price
    """ get input from user """
    while True:
        try:
            GTIN = int(input("input your gtin-8 number: "))
            return GTIN # Breaks the loop and returns the value
        except:
            print ("Oops! That was not a valid number.  Try again")


def search_user_input(product_data):
    global total_price
    repeat=""
    # Pass the csv data as an argument
    """ search csv data for string """
    keep_searching = True

    while keep_searching:
        gtin = get_user_input()
        for row in product_data:
            if row[0] == str(gtin):
                product = row[1]
                price = round(float(row[2]),2)
                return(product, price)
        while True:
            try:
                repeat = input("not in there? search again? If so (y), else press enter to continue")
                break
            except:
                print("please make sure you enter a valid string") 
        if repeat != 'y':
            keep_searching = False 
            return None 


def quantity():
    fileOne = open('receipt.csv', 'a')
    writer = csv.writer(fileOne)
    global total_price
    product_data = read_csv_file()
    matches = search_user_input(product_data)
    if matches: # Will not be True if search_user_input returned None
        print("apple")
        product, price = matches[0], matches[1]
        order = int(input("How much of {} do you want?".format(product)))
        values = [str(product), str(price), str(order*price)]
        writer.writerows((values,))
        total_price.append(order * price)
    continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))
if continue_shopping !=0 and continue_shopping !=1:
    if (continue_shopping == 0):
        fileOne.close()
        fileTwo = open("receipt.csv" , "r")
        reader = csv.reader(fileTwo)
        for row in reader:
            if row != None:
                print(row)
    elif continue_shopping==1:
        search_user_input()
        quantity()
quantity()

我将不胜感激对我的程序的任何帮助或指向正确方向的一般指示。谢谢你!


首先尝试删除函数中的所有“全局total_price”定义..(每次都会重置变量) 其次在你的函数 search_user_input() 中我会改变这一行price = round(float(row[2]),2) into price = float(row[2])(您稍后可以使用 format 以所需的格式显示最终的浮点值)。 第三,在你的函数数量()中你应该改变这一行if continue_shopping !=0 and continue_shopping !=1: into if continue_shopping ==0 or continue_shopping ==1:, 祝你好运

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让我的收据不再重复 的相关文章

随机推荐

  • cmd dir /b/s 加日期

    我正在 Windows XP 中寻找 cmd shell 命令 例如 dir b s 其中包含结果中每个文件的日期和时间值 所有数据 路径 文件名和日期 时间 都需要位于一行上 任何人都可以提供命令来完成此任务吗 谢谢 如果您只想要文件 f
  • 如何在 Greasemonkey 脚本中模拟按键?

    我在网上找到了很多关于如何使用的信息initEvent and dispatchEvent功能 https developer mozilla org en US docs DOM element dispatchEvent 但我一生都无法
  • 从小部件启动活动

    我正在尝试做一些本来应该很容易的事情 但这让我发疯 我试图在按下主屏幕小部件时启动一个活动 例如小部件的配置活动 我想我已经逐字逐句地遵循了 Android 开发者网站上的教程 甚至还有一些非官方教程 但我一定错过了一些重要的东西 因为它不
  • 从浏览器小程序使用 JDBC 时出现“访问被拒绝”

    我有一个 Java 小程序 可以查询 Oracle 数据库中的数据 当从 IDE 内部运行时 它运行得很好 但是 当我将它作为嵌入网页中的小程序运行时 我在类加载器中收到 访问被拒绝 错误 并且我根本不知道它对我的要求是什么 Sep 06
  • JSON IPHONE:如何发送 JSON 请求并从服务器提取数据?

    我对 JSON 几乎一无所知 我需要仅使用 iPhone 向服务器发送请求并读取来自它的数据 我尝试过使用杰森框架 https github com stig json framework这样做 但在阅读文档后 我无法弄清楚如何构造该对象并
  • 滚动到手机上的特定元素不起作用

    我有一个我想要的单击事件 当触发时 窗口滚动到特定坐标 div class col xs 12 responsiveView div class row div div class responsiveOrders div div clas
  • 远端关闭连接无响应

    我正在尝试使用以下代码从网页获取 HTML 源代码 import requests url https dictionary cambridge org us dictionary english arabic hi r requests
  • 提醒用户对应用程序进行评分的警报

    正如您可能在某些应用程序中看到的那样 会弹出一个警报 要求用户在 iTunes 中对应用程序进行评分 通常您可以选择的选项如下 当然 这将打开应用程序的评分页面 第二个选项是 不 谢谢 它会关闭警报 第三个选项通常是稍后 它会稍后显示警报
  • 与 INADDR_ANY 绑定

    如果我将套接字绑定到 INADDR ANY 我知道它将接受服务器上配置的任何 IP 上的传入连接 假设我在进行 bind 调用时配置了 1 个 IP 然后配置了一个新的 IP bind 是否也会接受向新配置的 IP 发起的连接 还是仅适用于
  • Aurelia 自定义属性中的双向绑定

    UPDATE 看起来这是一个已知的错误 https github com aurelia templated issues 253 https github com aurelia templating issues 253我将其留在这里是
  • 在项目 .d.ts 定义文件中使用 @types 定义

    我正在尝试为我的项目编写一个 d ts 文件来定义一些全局接口 但是 我在该定义文件中使用非全局库类型时遇到问题 特别是我试图引用的 RxJs 对我来说不起作用 我认为最有效的方法是使用三斜杠引用标签来导入 RxJ 的类型 但这不起作用
  • Masonry 插件:调整 div 大小不会导致重新洗牌

    将 Masonry 项目包裹在 1000px 宽的 div 中 我有一个按钮可以使用 jQuery 将 div 大小调整为 2000xaddClass 问题是 Masonry 不会重新排列项目来填充额外的 1000px 空间 我知道调整大小
  • PageViewController - 将变量传递给子视图

    我拥有的 我有一个 ViewController TutorialViewController 和一个 UIPageViewController TutorialPageViewController 故事板上还有 3 个带有 StoryBo
  • Paramiko 和伪 tty 分配

    我正在尝试使用 Paramiko 连接到远程主机并执行许多文本文件替换 i o e client exec command perl p i e s initial replaced g conf 其中一些命令需要作为 sudo 运行 这会
  • 用于创建工作项模板的 TFS 客户端 API?

    当然 很可能在 TFS 中创建工作项 获取工作项列表等 除此之外 我们还需要具有允许用户为各种文件类型创建自己的工作项模板的功能 TFS 客户端 API 是否能够将工作项模板上传到 TFS 服务器 有一种方法可以获取 XML 定义 Micr
  • 在函数内调用 div

    我正在尝试使用 jQuery mobile 1 4 3 创建一个类似于 iOS 警报视图的弹出窗口 我需要从 javascript 事件触发警告消息 例如带有 确定 按钮的确认消息 显示对 Web 服务的 ajax 响应调用 我的第一个问题
  • # jquery移动页面url中的字符

    为什么当我访问我的 jQuery 移动页面时 假设 page php 显示正常 但当我访问同一页面 page php someDetailsHere 时 它 只显示一个白色页面 我该如何解决这个问题 我使用第三方应用程序重定向到我的网页 添
  • 通过 Kafka 消费者重试维持订单保证

    我正在为基于 Kafka 的数据处理管道中的消费者重试设计一个架构 我们正在使用 Kafka 生产者和消费者 并且正在考虑重试主题 如果消费出错 将在这些主题上发送消息 将会有消费者以一定的节奏运行这些重试主题 我读了很多参考架构 但没有一
  • 渲染图表后,从图表对象更改 Highcharts 工具提示格式化程序

    我发现我可以使用 setData 更改系列 并且我知道我可以使用 setExtremes 修改最大值 但我无法弄清楚如何从图表对象设置工具提示格式化程序 我如何更新该字段 如果我有一个图表对象 如何更新其工具提示格式化程序属性 以及plot
  • 如何让我的收据不再重复

    这是一个程序 要求用户输入条形码 然后我的程序找到指定的产品 询问用户他们想要购买多少产品 如果他们想继续 计算总价 然后假设打印出收据 但是我的问题是收据重复其值并且没有四舍五入到小数点后两位 press 0 to stop shoppi