Jump to content

Python Help

Go to solution Solved by Guest,

To expand on the options presented by @straight_stewie

# f-string, which is only availible in 3.6 or higher
print(f"Your fuel cost is ${final_cost}")

# alternativly you could change it to a string mid line
print("Your fuel cost is $" + str(final_cost))

Hi all

 

I am having trouble with Python v3.6. Just so you know I am using PyCharm Community Edition.

 

I want to print a concatenated output (print with text and variables).

 

My code:

print("Your final cost is $" + final_cost)

 

But it gives me an error which says: (the error is in all red though (well apart from the path to file))

 

#line 50 is the line of code above

Traceback (most recent call last):
  File "path to my file", line 50, in <module>
    print("Your fuel cost is $" + final_cost)
TypeError: must be str, not float

 

I know I can replace the plus with a comma but it puts a space between the $ and the final_cost variable which I do not want.

 

How can I output this concatenated print without this error?

 

Thanks

 

Hugs

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/896269-python-help/
Share on other sites

Link to post
Share on other sites

5 hours ago, Hugs12343 said:

How can I output this concatenated print without this error?

There are generally two ways to accomplish this, which I will outline below (I assume that your fuel cost is a float):

If it was known ahead of time that fuel_cost only had two digits after the decimal point then there would be other options we could use.
 

 

Traditional string formatting

print("Your fuel cost is $%(0:.2f)", final_cost)

 

Newer string formatting

print("Your fuel cost is ${0:.2f}", .format(final_cost))

 


 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11043436
Share on other sites

Link to post
Share on other sites

10 hours ago, tjcater said:

To expand on the options presented by @straight_stewie


# f-string, which is only availible in 3.6 or higher
print(f"Your fuel cost is ${final_cost}")

# alternativly you could change it to a string mid line
print("Your fuel cost is $" + str(final_cost))

Thank-you very much. The f-string is the easiest thing to remember

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11045984
Share on other sites

Link to post
Share on other sites

2 hours ago, Hugs12343 said:

Thank-you very much. The f-string is the easiest thing to remember

Np, just remember to not get overly reliant on it. Its my preferred way of doing it, but there are some situations where you will need an alternative as there are several capabilities that it lacks over .format() (I tend to forget what those were as I don't run into them much, but just remember that it does lack a couple of things.)

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11045997
Share on other sites

Link to post
Share on other sites

6 hours ago, geo3 said:

Don't store money in floats or doubles. Use the decimal class. 

The easier way is to just string it at the end like this:

 

20 hours ago, tjcater said:

# alternativly you could change it to a string mid line print("Your fuel cost is $" + str(final_cost))

 

PC Specs:

CPU: Intel Core i7-12700K 3.6 GHz 12-Core
CPU Cooler: Corsair iCUE H150i ELITE CAPELLIX 75 CFM Liquid CPU Cooler
Motherboard: Asus ROG STRIX Z690-E GAMING WIFI ATX LGA1700
RAM: Kingston FURY Beast 32 GB (2 x 16 GB) DDR5-5200 CL40
Storage: Boot Drive: Samsung 960 Evo 250GB M.2 NVMe SSD

               Other Storage: Mass Storage: Western Digital Caviar Blue 1TB 7200 RPM, Western Digital Caviar Blue 2TB 5400 RPM, Scratch Disk: Intel X25-E SSDSA2SH032G1 32GB SATA II SSD, Backup Drive: Seagate ST3160318AS 160GB HDD
GPU: Asus GeForce RTX 3080 Ti 12 GB ROG STRIX GAMING OC
Case: Corsair 5000D AIRFLOW ATX Mid Tower
PSU: Silverstone Strider Platinum S 1000 W 80+ Platinum Certified Fully Modular ATX
OS: Windows 11 Pro 64-Bit
Monitors: Primary: Samsung S34E790C 34" 3440*1440 60 Hz UWQHD; Secondary: LG 34UM58-P 34" 2560*1080 75 Hz UWFHD; Tertiary: BenQ GL2460 24" 1920*1080 60 Hz FHD

Keyboard: Corsair K70 Mk. 2 RGB Gaming Keyboard - Black

Mouse: Corsair M65 Pro RGB FPS Gaming Mouse - Black, Logitech MX Master 3

Headphones: Corsair VOID PRO Surround Cherry 7.1ch

Speakers: Logitech Z213 7W 2.1ch

 

Laptop:

Asus Zenbook Pro 15 (UX535Li-E2018T) with Intel Core i7-10750-H 12MB @ 2.60GHz (Turbo @ 5.0 GHz), 16GB (2 x 8GB) DDR4 2933 MHz SODIMM and Intel(R) UHD Graphics; NVidia Geforce GTX 1650-Ti with Max-Q Design, using WDC NVMe PC SN730 SDBPNTY-1T00-1102, on a 96-Wh battery

 

NAS Specs:

Make & Model: QNAP TS-1277

CPU: AMD Ryzen 5 1600 @Stock

Hard Drives: x8 WD Red 2TB

SSDs (2.5"): x1 Samsung 850 Evo 250GB V-NAND (cache drive)

M.2 SSDs: None

RAID Configuration: RAID 6 (excluding SSD)

Total Storage: 12TB

Expansion Cards: None

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11047067
Share on other sites

Link to post
Share on other sites

2 hours ago, Hugs12343 said:

The easier way is to just string it at the end like this:

 

 

No! That's wrong. Money should never be in a float or double at any point, you are going to get rounding errors. Use decimal.

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11047448
Share on other sites

Link to post
Share on other sites

6 hours ago, Hugs12343 said:

The easier way is to just string it at the end like this:

They weren't trying to say the method of presenting the variable is wrong, just that floating points are bad for use with money. I don't think you need to worry much unless you are dividing or working in a professional environment, but I'll further explain what they meant.

# When you define a variable this way in python, its automatically a float/double
final_value = 8.99
# The reason this can be bad is that when you work with larger values, things become inaccurate
final_value = 10 / 3
# After a certain digit numbers start to become random

# Run 1
print(final_value)
# 3.33333333333333333376546487464867948636
# Run 2
print(final_value)
# 3.33333333333333333321774386415188348748

You can see why this can be an issue, but if you are just now learning how to program, I wouldn't stress over it for now.

If you want to know more about the decimal class that was mentioned before, check out the documentation here.

Also a page for more detail about the rounding issues (Link)

Link to comment
https://linustechtips.com/topic/896269-python-help/#findComment-11048365
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×