TypeError:不支持的格式字符串传递给了NoneType.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu(我不知道这是什么意思)

2024-10-03 11:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码,我不知道这个错误意味着什么。任何人请告诉我哪里做错了,什么应该是正确的代码。非常感谢帮助

代码:

cart = ['S/n'," "*10, 'Items', " " * 15, "Quantity", " " * 8, "Unit Price", " " * 6, "Price"]
total_pricee = 0
pricee = 0
count=1
cart_number=[count]
def invalid_input(Quantity):
    while Quantity >= '5' or Quantity < '0':
        Quantitiy = input("Please key in a valid quantity(Between 1 to 4):")
        if Quantity <= '5' and Quantity > '0':
            return Quantity
            break
        break
    while not Quantity.isdigit():
        Quantity = input('Invalid input.Please enter a valid input:')
        while Quantity.isdecimal() == False:
            break

def add_to_cart(name, Quantity, price):
    global total_pricee, pricee,count
    cart.append('\n')
    cart.append('{:<10s}'.format(str(count)+'.'))
    cart.append('{:^10s}'.format(name))
    cart.append('{:^30s}'.format(Quantity))
    cart.append('{:^10s}'.format('$' + '{:.2f}'.format(float(price))))
    pricee = '{:.2f}'.format(float(Quantity) * price)
    cart.append('{:^23s}'.format('$' + str(pricee)))
    total_pricee += float(pricee)
    count = count +1
while True:
    print('[1] Water')
    print('[2] rice')
    print('[3] ice')
    print('[0] View Cart')
    opt = input("Select option:")
    if opt > '3' or opt < '0':
        print("Select valid option!")
    if opt == '3':
        qunt = input("How many would you like?")
        qunt=invalid_input(qunt)
        nam3 = "Ice"
        add_to_cart(nam3, qunt, 2)
    if opt == '1':
        qunt2 = input("How many would you like?")
        quan2=invalid_input(qunt2)
        nam2 = "Water"
        add_to_cart(nam2, qunt2, 3)
    if opt == '2':
        qunt1 = input("How many would you like?")
        qunt1=invalid_input(qunt1)
        nam1 = "Rice"
        add_to_cart(nam1, qunt1, 5)
    if opt == "0":
        print(*cart)
        print("Total price until now:", "$" + '{:.2f}'.format(total_pricee))
        print('Would you like to check out?')
        print('[1] Yes')
        print('[2] No')
        checkout=input("Please select an option:")
        if checkout=='1':
            print('You have bought',count,'items')
            print("Please pay""$" + '{:.2f}'.format(total_pricee))
            print('Thank you for shopping with us!')
            exit()

我得到这个错误:

TypeError: unsupported format string passed to NoneType.__format__

我只是说学习python,我不知道这个错误是什么意思。任何人请告诉我哪里我做错了,什么应该是正确的代码。非常感谢您的帮助,非常感谢


Tags: to代码youformatinputifcountquantity
1条回答
网友
1楼 · 发布于 2024-10-03 11:24:38

查看使用.format方法的代码行。至少在其中一个中,您有一个空值,并且您正在尝试对其使用.format方法-这是您无法做到的。 因此出现错误:NoneType.format

在每次操作之前打印所有变量,或者添加一些逻辑来检查空值,例如

if name != None:
    cart.append('{:^10s}'.format(name))

相关问题 更多 >