![]() |
Quick question/help regarding my variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Quick question/help regarding my variable (/thread-37127.html) |
Quick question/help regarding my variable - Extra - May-04-2022 Hello, My sellPrice variable won't work because it's taking price as a string and trying to multiply it to a number. I tried forcing the price to be an int [price = int(input("Item Price $: "))] But I get: How do I get this to work?(It's probably a quick fix, but I'm drawing a blank right now) Thanks in advance. The code right now: #Store items and related info in a list #Create an empty list Item = [] #Elements for the list name = (input("Item Name: ")) quantity = (input("Item Quantity: ")) price = (input("Item Price $: ")) sellPrice = price * 1.10 ItemInfo = ("Name: " + name, "Quantity: " + quantity, "Price: $" + price, "Sell Price: $" + sellPrice) Item.append(ItemInfo) #Appened the ItemInfo to the Item list print(Item) #Print the list RE: Quick question/help regarding my variable - Larz60+ - May-04-2022 use float(price) * 1.10
RE: Quick question/help regarding my variable - Extra - May-04-2022 (May-04-2022, 11:36 PM)Larz60+ Wrote: use I tried that and get this error (It's unable to concatenate): Anyway to work around/fix this?Code: #Store items and related info in a list #Creatie an empty list Item = [] #Elements for the list name = (input("Item Name: ")) quantity = (input("Item Quantity: ")) price = (input("Item Price $: ")) sellPrice = float(price) * 1.10 description = (input("Item Description: ")) category = (input("Item Category: ")) location = (input("Item Location: ")) ItemInfo = ("Name: " + name, "Quantity: " + quantity, "Price: $" + price, "Sell Price: $" + sellPrice, "Description: " + description, "Category: " + category, "Location: " + location) Item.append(ItemInfo) #Appened the ItemInfo to the Item list print(Item) #Print the list RE: Quick question/help regarding my variable - bowlofred - May-05-2022 You can add two numbers together, or you can add two strings together. You can't add a number and a string. On lines 16, 17, and 18, you're adding (concatenating) 2 strings. On line 19, you're adding a string "Sell Price: $" and a number sellPrice .It looks like ItemInfo is just a bunch of strings. Is that useful to you? I'd probably leave the prices as numbers and only shove the description on them later when they're printed out. You can convert your number to string if you do want to concatenate it. "mystring" + str(4)
RE: Quick question/help regarding my variable - deanhystad - May-05-2022 price and sellprice should be numbers. They should not have dollar signs. You could make your item a class and provide a __str__() method that formats the output any way you like. from dataclasses import dataclass @dataclass class Item: name:str quantity:int price:float category:str = "" location:str = "" description:str = "" sellprice:float = None def __post_init__(self): if self.sellprice is None: self.sellprice = self.price * 1.1 def __str__(self): return "\n".join(( f"Name : {self.name}", f"Description : {self.description}", f"Quantity : {self.quantity}", f"Price : ${self.price:0.2f}", f"Sell Price : ${self.sellprice:0.2f}", f"Category : {self.category}", f"Location : {self.location}" )) @classmethod def input(cls): return cls( input("Item Name: "), int(input("Item Quantity: ")), float(input("Item Price $: ")), category = input("Item Category: "), location = input("Item Location: "), description = input("Item Description: ")) items = [] items.append(Item.input()) for item in items: print(item) RE: Quick question/help regarding my variable - Extra - May-06-2022 Thanks that helped. Now is there a way to loop it like so: (This is the example) #--------------------------------------------------------------------------- # Loop Input to take in several lists #--------------------------------------------------------------------------- #Creatie an empty list Item = [] #Keep taking in user inputs over and over and store that info into new/diffrent lists #And display all the lists whens the user hits 'Q' to quit while True: #Elements for the list name = (input("Item Name (Q to Quit): ")) if name == "Q": break price = (input("Item Price: ")) quantity = (input("Item Quantity: ")) description = (input("Item Description: ")) category = (input("Item Category: ")) ItemInfo = (name,price,quantity,description,category) Item.append(ItemInfo) #Append the ItemInfo to the Item list. print(Item) #Print the list #---------------------------------------------------------------------------So where should I put the while True statement so it can loop? (This is the code I would like to to loop so I can make several lists like the example code above) from dataclasses import dataclass #Define variables for the Item Info that the user enters @dataclass class ItemInfo: name:str quantity:int price:float sellprice:float = None description:str = "" category:str = "" location:str = "" def __post_init__(self): if self.sellprice is None: self.sellprice = self.price * 1.1 #Returns the results def __str__(self): return "\n".join(( f"Name : {self.name}", f"Quantity : {self.quantity}", f"Price : ${self.price:0.2f}", f"Sell Price : ${self.sellprice:0.2f}", f"Description : {self.description}", f"Category : {self.category}", f"Location : {self.location}" )) #Take user input @classmethod def input(cls): return cls( input("Item Name: "), int(input("Item Quantity: ")), float(input("Item Price $: ")), description = input("Item Description: "), category = input("Item Category: "), location = input("Item Location: ")) #Create an empty list called Items items = [] #Appened the ItemInfo to the Item list items.append(ItemInfo.input()) #Print The List print('\n') print('--------------------') for item in items: print(item) print('--------------------') |