Python Forum
Quick question/help regarding my variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick question/help regarding my variable
#1
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
Reply
#2
use float(price) * 1.10
Reply
#3
(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
Reply
#4
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)
Reply
#5
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)
Reply
#6
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('--------------------')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Quick Question about Dictionaries Extra 6 1,831 Apr-29-2022, 08:34 PM
Last Post: Extra
  quick question/excel+python zarize 1 2,267 Dec-06-2019, 05:38 PM
Last Post: Larz60+
  SELECT statement query question using a variable DT2000 2 3,033 Feb-23-2019, 07:35 AM
Last Post: DT2000
  A quick question teczone 4 3,084 Sep-06-2018, 03:44 PM
Last Post: snippsat
  Completely new to coding - quick question Oster22 1 2,700 Jun-19-2018, 08:42 PM
Last Post: Larz60+
  Quick help! Ellisrb 2 2,763 May-02-2018, 11:21 AM
Last Post: ThiefOfTime
  ldap3 question, using a variable in search string gentoobob 1 2,889 Apr-28-2018, 11:16 AM
Last Post: gentoobob
  Quick Lists Question (Count Occurences) EwH006 9 8,044 Nov-16-2016, 04:33 PM
Last Post: Larz60+
  quick question about deleting an object from memory LavaCreeperKing 5 5,838 Nov-12-2016, 04:05 PM
Last Post: LavaCreeperKing
  quick way to convert in both 2 and 3 Skaperen 10 8,768 Nov-03-2016, 04:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020