Python Forum
Outputting a float value in a print() statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outputting a float value in a print() statement
#1
For some reason, it's not letting me concatenate a float in my print statement on line 6:

#!/usr/bin/env python3
#BasicUserInterface.py

#MACH 1 = 761.2071 mph
#1 mph = MACH 0.0013

def calculateMACH(mph):
    mach = mph * 0.0013
    return mach

def calculateTime(mph, distance):
    time = distance / mph
    return time

def calculateMinutes(mph, distance):
    time = distance / mph
    milesPerMinute = time / 60
    return milesPerMinute

def main():
    choice = "y"
    while choice.lower() == "y":
        mph = float(input("Enter speed in mph:"))
        distance = float(input("Enter number of miles to destination:"))        

        mach = calculateMACH(mph)
        time = calculateTime(mph, distance)
        milesPerMinute = calculateMinutes(mph, distance)

        if mph > 761.2071:
            print("You are traveling faster than the speed of sound!")

        print("You are traveling at MACH " + str(mach))
        print("At that speed, you should reach your destination in " +
              str(round(time, 2)) + " hours, or " +
              str(round(milesPerMinute, 2) + " minutes."))

        choice = input("Do you want to continue? (y/n)")
        print()

    print("Bye!")

if __name__ == "__main__":
    main()
Error:
===== RESTART: I:/Python/Python36-32/SamsPrograms/BasicUserInterface.py ===== Enter speed in mph:60 Enter number of miles to destination:60 You are traveling at MACH 0.078 Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/BasicUserInterface.py", line 44, in <module> main() File "I:/Python/Python36-32/SamsPrograms/BasicUserInterface.py", line 36, in main str(round(milesPerMinute, 2) + " minutes.")) TypeError: unsupported operand type(s) for +: 'float' and 'str'
What's wrong?
Reply
#2
Your closing bracket is not in place. See the prev. str() function.

It's better though to use string formatting instead of concatenation.
print("At that speed, you should reach your destination in {} hours, or {} minutes.".format(str(round(time, 2)), str(round(milesPerMinute, 2)))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Jan-11-2018, 07:32 AM)wavic Wrote: It's better though to use string formatting instead of concatenation.
Python Code: (Double-click to select all)
1

print("At that speed, you should reach your destination in {} hours, or {} minutes.".format(str(round(time, 2)), str(round(milesPerMinute, 2)))

well, it is good to use string formatting in its full power

>>> hours = 1.23456
>>> print('you will reach your destination in {:.3f} hours, or {:.2f} minutes'.format(hours, hours*60))
you will reach your destination in 1.235 hours, or 74.07 minutes
format specification mini language
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 293 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Reading a directory and outputting as sound loungeroom 0 1,228 Jul-30-2021, 09:26 PM
Last Post: loungeroom
  Why doesn't this print statement work? stylingpat 10 5,761 Mar-23-2021, 07:54 PM
Last Post: buran
  Opening file and outputting its text content example leodavinci1990 1 1,887 Oct-12-2020, 05:33 AM
Last Post: buran
  print either int or float - How? Denial 1 1,387 Sep-07-2020, 04:12 PM
Last Post: DPaul
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,834 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Outputting Sorted Text files Help charlieroberrts 1 1,739 Jul-05-2020, 08:37 PM
Last Post: menator01
  Taking brackets out of list in print statement pythonprogrammer 3 2,411 Apr-13-2020, 12:25 PM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,565 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  printing a list contents without brackets in a print statement paracelx 1 2,141 Feb-15-2020, 02:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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