Python,从函数返回失败,卡在While循环中

Python,从函数返回失败,卡在While循环中,python,while-loop,Python,While Loop,我不熟悉python和编码,对于一个学校项目,我必须打印一张简单的收据。我使用了一些函数,其中一个函数询问购买物品的价格,使用了whilenot循环。输入循环,直到用户输入“0” 问题是,如果我输入一个带有三位或更多小数位的采购项目价格(例如19.999)。当他们在此错误输入之后输入0时,Return命令似乎卡在While Not循环中。当代码到达Return命令时,执行跳转到whilenot循环并再次返回,然后在whilenot循环的一半跳到'prices=str(prices)'并正常继续。

我不熟悉python和编码,对于一个学校项目,我必须打印一张简单的收据。我使用了一些函数,其中一个函数询问购买物品的价格,使用了whilenot循环。输入循环,直到用户输入“0”

问题是,如果我输入一个带有三位或更多小数位的采购项目价格(例如19.999)。当他们在此错误输入之后输入0时,Return命令似乎卡在While Not循环中。当代码到达Return命令时,执行跳转到whilenot循环并再次返回,然后在whilenot循环的一半跳到'prices=str(prices)'并正常继续。结果是,我们无法以“0”的预期方式退出程序

下面是函数

def shop_list(): #this definition asks for the prices of the items, adds the input to a list and loops, until 0 is entered, which stops the loop.
    while True:
        try:
            prices = float(input("Please input a value. Enter 0 to print receipt: ")) # the input that allows the customer to enter their prices
            zero_check.append(prices)
            if prices == 0:
                if len(zero_check) == 1:
                    if zero_check[0] == 0:
                        exit_ask()
                    else:
                        'do nothing' #this is a place holder line, something must be here but we dont need anything here
                else:
                    'do nothing'
            else:
                'do nothing'
        except ValueError: #if the input creates an error, do the below
            print("\nERROR, please enter a valid number.\n") #this error message will come up if you input anything other than a number
            continue #loops the back to the 'try'
        else:
            break #breaks the While True loop

    if prices != 0: 
        number = None
        while not number:
            prices = str(prices) #converting the price to a string so we can split it
            string_number = prices.split(".") #splitting the price at the decimal point
            if len(string_number[1]) > 2: #if there is more than two decimal points, print an error
                print ("\nERROR: Too many decimal places!\n")
                prices = float(prices)
                shop_list()
            else:
                number = float(prices)

            prices = str(prices)
            price_lnth = len(prices)

            if price_lnth > 15:
                print ('\nERROR, too many numbers.\n')
                shop_list()
            else:
                prices = float(prices)

            if prices > 0: #if the input was valid then this will run
                shopplist.append(prices) #this is what adds a price into a list
                shop_list() # loops back to the start of this definition              
            elif prices < 0:
                print('\nERROR, no negative numbers.\n')
                shop_list() # loops back to the start of this definition   

    else:
        'do nothing'
    return
def shop_list():#此定义询问商品的价格,将输入添加到列表并循环,直到输入0,这将停止循环。
尽管如此:
尝试:
价格=浮动(输入(“请输入一个值。输入0打印收据:)#允许客户输入其价格的输入
零支票追加(价格)
如果价格=0:
如果len(零检查)=1:
如果零检查[0]==0:
退出
其他:
“什么也不做”#这是一条占位符线路,这里一定有东西,但我们不需要任何东西
其他:
“什么也不做”
其他:
“什么也不做”
ValueError除外:#如果输入产生错误,请执行以下操作
打印(“\n错误,请输入一个有效的数字。\n”)#如果您输入的不是数字,则会出现此错误消息
继续#循环返回“尝试”
其他:
中断#中断While True循环
如果价格合理!=0: 
数字=无
虽然不是数字:
prices=str(prices)#将价格转换为字符串以便拆分
string_number=prices.split(“.”)#在小数点处拆分价格
如果len(string_number[1])>2:#如果有两个以上的小数点,则打印一个错误
打印(“\n错误:小数位太多!\n”)
价格=浮动(价格)
店铺清单()
其他:
数字=浮动(价格)
价格=str(价格)
价格=长度(价格)
如果价格>15:
打印('\n错误,数字太多。\n')
店铺清单()
其他:
价格=浮动(价格)
如果价格>0:#如果输入有效,则此操作将运行
shopplist.append(prices)#这是将价格添加到列表中的内容
shop_list()#循环到此定义的开头
elif价格<0:
打印('\n错误,没有负数。\n')
shop_list()#循环到此定义的开头
其他:
“什么也不做”
回来

当出现错误时(也在正常逻辑中),可以递归调用
shop\u list()
。然后输入0时,第二次调用将返回,但原始调用将继续进行。我建议只使用循环,而不是递归,或者只使用递归(但请记住,递归会占用堆栈空间)。

首先,正确缩进代码。您不需要
其他短语:“什么都不做”
短语。如果有一个
if
而没有
else
,没有什么错。一个优点可能是结构明确,这样编辑器中的缩进不会意外地将以下
else:
置于错误的级别。但我认为增加的混乱是不值得的。当您有时确实需要一行不起任何作用的代码时,另一种选择是
pass
。非常感谢您的回复!对于如何删除递归并仅依赖循环,您有什么建议吗?我对编码很陌生,对别人来说很明显的事情对我来说并不明显。任何反馈都将不胜感激!