Posts: 232
Threads: 81
Joined: Nov 2021
May-04-2022, 11:33 PM
(This post was last modified: May-04-2022, 11:33 PM by Extra.)
Hello,
My sellPrice variable won't work because it's taking price as a string and trying to multiply it to a number.
Error: File "ListTest.py", line 10, in <module>
sellPrice = price * 1.10
TypeError: can't multiply sequence by non-int of type 'float'
I tried forcing the price to be an int
[ price = int(input("Item Price $: "))]
But I get:
Error: Traceback (most recent call last):
File "ListTest.py", line 18, in <module>
"Price: $" + price,
TypeError: can only concatenate str (not "int") to str
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
Posts: 12,025
Threads: 484
Joined: Sep 2016
Posts: 232
Threads: 81
Joined: Nov 2021
(May-04-2022, 11:36 PM)Larz60+ Wrote: use float(price) * 1.10
I tried that and get this error (It's unable to concatenate):
Error: Traceback (most recent call last):
File "ListTest.py", line 19, in <module>
"Sell Price: $" + sellPrice,
TypeError: can only concatenate str (not "float") to str
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
Posts: 1,583
Threads: 3
Joined: Mar 2020
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)
Posts: 6,780
Threads: 20
Joined: Feb 2020
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)
Posts: 232
Threads: 81
Joined: Nov 2021
May-06-2022, 12:01 AM
(This post was last modified: May-06-2022, 12:02 AM by Extra.)
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('--------------------')
|