Python Forum

Full Version: Property price calculation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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
(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}")
>>> 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.