Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Property price calculation
#1
Hi. I'm a beginner stuck with a problem and need help please. I need to calculate the total property size ( width and length ) and then the total cost of the property. I need to print the final property sum in the shell

I've managed the following very basic setup:
This is basically how it should work with the given parameters.
>>> length = 15
>>> width = 10
>>> price = 8.99
>>> total_m2 = length * width
150
>>> print(total * price)
1348.5


But, I'm trying to implement the following:
>>> squaremeters = "m2"
>>> euro = "€"
>>> length = 15
>>> width = 10
>>> price = euro + str(8.99) + squaremeters
'€8.99m2'
>>> totalm2 = length * width
150
>>> total_m2 = str(totalm2) + squaremeters
'150m2'
>>>print = (total_m2 * price)   
TypeError: can't multiply sequence by non-int of type 'str'


I've tried using multiple data types, but i keep getting the same or similar error. Is it just not possible or have I gone completely off the beaten track here? So confused!

Thanks in advance
Reply
#2
I assume you want the final output to be
Output:
€ 1348.5
. If so, you can use fstring. Try:

print(f' € {length*width*price}')
Total_m2 and price are both str type and because they contain chars, you can't convert them to int or float for use in multiplication.

More details: https://www.python.org/dev/peps/pep-0498/

You can do the same for the area.
Reply
#3
Thanks for the quick reply @palladium. Sorry I explained badly here. Let me try it like this:

length = int(input("Please add the length of the property: "))
width = int(input("Please add the width of the property: "))
price_per_m2 = float(input("Please add the price per square meter: "))

total_price = length * width * price_per_m2   
print("The price of the property is", total_price)
The price of the property is _ _ _ 
Is there anyway I can add "€" to the price?

Thanks
Reply
#4
(Jul-15-2020, 02:08 PM)oli_action Wrote: Is there anyway I can add "€" to the price?
You use what @palladium showed,it's called f-string look at this tutorial.
length = int(input("Please add the length of the property: "))
width = int(input("Please add the width of the property: "))
price_per_m2 = float(input("Please add the price per square meter: "))
total_price = length * width * price_per_m2

print(f"The price of the property is € {total_price}")
Reply
#5
>>> price = euro + str(8.99) + squaremeters
'€8.99m2'
>>> totalm2 = length * width
150
>>> total_m2 = str(totalm2) + squaremeters
'150m2'
>>>print = (total_m2 * price)   
Since you converted price as a string , it throws the error of
Error:
TypeError: can't multiply sequence by non-int of type 'str'

You can format your output to f-string in final print statement as how @palladium suggested.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the price based on industry and number of transactions chandramouliarun 0 898 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,686 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  class - calculate total price of copies 3lnyn0 1 1,527 Dec-12-2021, 11:43 PM
Last Post: BashBedlam
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,095 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,565 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,256 Jun-15-2021, 03:48 PM
Last Post: snippsat
  An important question is how to create a zigzag in price data? epsilon 0 1,276 Nov-18-2020, 08:06 PM
Last Post: epsilon
  Can property getters and setters have additional arguments? pjfarley3 2 2,993 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Use of @property decorator ruy 16 6,418 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,019 Dec-17-2019, 08:00 PM
Last Post: jasonashaw

Forum Jump:

User Panel Messages

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