Я новичок в Python и кодировании, и для школьного проекта я должен распечатать простую квитанцию.Я использую несколько функций, и одна из них, запрашивающая цены на купленные товары, использует цикл «Пока не получилось».Ввод осуществляется до тех пор, пока пользователь не введет «0».
Проблема заключается в том, что если я введу цену купленного товара с тремя или более десятичными знаками (например, 19,999).Когда они вводят 0 после этого неправильного ввода, команда Return, похоже, застревает в цикле while Not Loop.Когда код попадает в команду Return, выполнение переходит к циклу While Not Loop и обратно, а затем проходит половину цикла While Not Loop до «Price = Str (цен)» и продолжается в обычном режиме.В результате мы не можем выйти из программы заданным образом, используя «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