首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:传递给NoneType.__format__的不受支持的格式字符串(我不知道这意味着什么)

TypeError:传递给NoneType.__format__的不受支持的格式字符串(我不知道这意味着什么)
EN

Stack Overflow用户
提问于 2020-06-14 18:16:21
回答 1查看 629关注 0票数 0

这是我的代码我不知道这个错误means.Anyone请告诉我我哪里做错了,什么应该是正确的code.Help非常感谢。

代码:

代码语言:javascript
复制
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()

我得到了这个错误:

代码语言:javascript
复制
TypeError: unsupported format string passed to NoneType.__format__

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

EN

回答 1

Stack Overflow用户

发布于 2020-06-14 18:25:38

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

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

代码语言:javascript
复制
if name != None:
    cart.append('{:^10s}'.format(name))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62371145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档