【问题标题】:TypeError: unsupported format string passed to NoneType.__format__(I do not know what this means)TypeError:不支持的格式字符串传递给 NoneType.__format__(我不知道这是什么意思)
【发布时间】:2020-10-03 20:34:07
【问题描述】:

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

代码:

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,我不知道这个错误是什么意思。任何人都请告诉我我在哪里做错了,什么应该是正确的代码。非常感谢您的帮助。非常感谢!

【问题讨论】:

  • 请将完整的错误回溯添加到您的问题中。
  • 欢迎来到 SOF!错误应该是 line-number ,请也添加这些详细信息
  • 2 个错误。第 1 行:第 53 行,在 add_to_cart(nam1, qunt1, 5) 第 25 行,在 add_to_cart cart.append('{:^30s}'.format(Quantity)) 类型错误:传递给 NoneType 的格式字符串不受支持。 __格式__

标签: python python-3.x list error-handling


【解决方案1】:

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

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

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

【讨论】:

  • 他的意思是喜欢'...'.format(variable) 的调用之一是错误的。这是错误的,因为传递的变量为 null 或未定义。他的建议是在 format 行之前打印格式化的值以调试未定义的值并添加条件以防止调用未定义值的 format。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2017-12-30
  • 2021-10-31
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多