字典命令的字符串参数错误

2024-10-06 12:46:00 发布

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

我正在尝试用python创建一个基本的在线商店。但每当我试图“买”一件东西时,它就会显示出我的字典有错误或我不确定的东西

错误:users[n][“Transactions”]=users[n][“Transactions”]+str(name_f,“bunded”,quanti,“of”,final[choice*3],“总价$”+price) TypeError:str()最多接受3个参数(给定6个)

    coun = 0

    users = [{"name":"Jack","username":"ja", "cc":'12345',"email":'whwhwwhh', "code": '111', "Transactions": ""}]

    def sign_in():
    username = input("Enter username")
    for i in range (len(users)):
            for x in users[i].values():
                if x == username:
                    pin = input("Enter pin")
                    if pin == users[i].get("code"):
                        print("Welcome", users[i].get("name"))
                        menu(username,users[i].get("name"))
                        break
                else:
                     print("Wrong pin")
                     sign_in()

     def menu (usern, names_f):
     global coun
     if coun == 0:
     order = ''
     total = 0
     for i in range (len(categories)):
            print(str(i+1)+".", categories[i])
            choice = int(input("Choose a category by typing the number beside the categories name."))-1
            print("Items in this list are")
            print("Itemname \t Price \t Stock")
            final = location[choice]
            for c in range((int(len(final)/3))):
                print(str(c+1)+'.',str(final[c*3]),"\t",'$'+str(final[c*3+1])), "\t", str(final[(c*3)+2])
            choice = int(input("Which item (Type number on left of the item name)"))-1
            while True:
                quanti = int(input("How many do you want to buy"))
                if quanti > final[choice*3+2]:
                    print("Sorry your request for", quanti, "Is more than we have at the store please try again")
                    continue
                else:
                    price = str(quanti*final[choice*3+1])
                    final[choice*3+2] = final[choice*3+2]-quanti
                    print("Thank you for your purchasing",quanti,"of", final[choice*3], "Your total price of this buy is", '$'+price)
                    for n in range (len(users)):
                        if usern == users[n].get("username"):
                            users[n]["Transactions"] = users[n]["Transactions"] + str(names_f, "bought", quanti, "of", final[choice*3], "with a total price of $"+price)
                            order += str(quanti, 'of', final[choice*3])
                            price += int(price)
                        done = input("Do you want to check out then type '1' if you want to continue type '2'")
                        if done == '1':
                            print("Thank you")
                            print ("Invoice:", order, "/n total price (HKD) $"+str(price))
                        else:
                            coun += 1
                            menu(usern,names_f)   

Tags: ofnameinforinputifusernameusers
2条回答
variable_name = users[n]["Transactions"] + str(names_f) + "bought" + str(quanti) + "of" + str(final[choice*3]) + "with a total price of $"+ str(price)
users[n]["Transactions"] = variable_name

您可能需要在某个地方声明变量名称。 问题是str的用法如下 str(对象,编码=编码,错误=错误) 但当你传递逗号时,它就作为另一个参数来计算。 另外,我不确定你是否需要我的解决方案中的所有str

str是一个类,如docs中所述,最多可以向其传递3个参数:

class str(object=b'', encoding='utf-8', errors='strict')

此外,它还说明了它的功能:

Return a string version of object. If object is not provided, returns the empty string.

这意味着它用于将其他类型强制转换为字符串。因此,您需要单独转换每个int:

users[n]["Transactions"] = users[n]["Transactions"] + str(names_f) + " bought " + str(quanti) + " of " + str(final[choice*3]) + " with a total price of " + str(price)

注意每个字符串前后的空格。或者,您可以格式化字符串:

users[n]["Transactions"] = users[n]["Transactions"] + '%s bought %s of %s with a total price of %s' % (names_f, quanti, final[choice*3], price)

作为旁注,值得检查第一笔交易完成后会发生什么。如果键Transactions还不存在,则需要在访问它之前添加初始值

我通常这样做:

if key not in dict_:
  dict_[key] = 'my initial value'

dict_[key] += 'add my stuff'

另一种解决方案是使用get方法,该方法允许您添加默认值:

dict_.get(key, 'default')

请注意,这不会将密钥添加到字典中,这意味着稍后尝试访问其值仍将导致密钥错误

相关问题 更多 >