Python 我没有获得代码的预期输出

Python 我没有获得代码的预期输出,python,csv,Python,Csv,我会尽量简化,因为我的代码是为用户设计的,用户可以使用条形码从数据库输入多个产品,从而使我的程序输出他们订购的产品的名称和产品的总价 然而,当代码第一次运行时,我的预期输出是 GTIN = int(input("input your gtin-8 number: ")) return GTIN # Breaks the loop and returns the value except: print ("Oops! That was

我会尽量简化,因为我的代码是为用户设计的,用户可以使用条形码从数据库输入多个产品,从而使我的程序输出他们订购的产品的名称和产品的总价

然而,当代码第一次运行时,我的预期输出是

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")
但是,由于某些原因,此代码首先在代码末尾作为起始行运行

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:
    print("make sure you enter a valid number")
如果您对我的代码有任何有用的意见或批评,我将不胜感激

如果您感兴趣,这里是作为一个实体的代码本身。谢谢你的帮助

import csv 
import locale
from decimal import *
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)))
        TWOPLACES = Decimal(10) ** -2
        subt = order * pricea
        subtotal = Decimal(subt).quantize(TWOPLACES)
        values = [str(product), str(price), str(subtotal)]
        print('values are ',values)
        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:
    print("make sure you enter a valid number")
    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()
quantity()

当您创建一个函数时(def就是这样做的),它不会做任何事情,除非您实例化该函数。因此,例如,如果您想在询问此人是否购物完毕之前运行read_csv_file()函数,您必须这样做。(或类似的东西)


我希望这有帮助

“然而,出于某种原因,这段代码首先在代码末尾作为起始行运行。”当然是这样。这是不是函数定义的一部分的第一条语句(导入除外)。你实际上并没有调用你定义的函数。这正是我想要的!非常感谢,先生
#all your functions are above this snippet.

read_csv_file()

continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))