### Thread: Float, string and integer

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
2
Rep Power
0

#### Float, string and integer

I've been trying to do this task as a bit of personal practice in python but I keep getting this error:
#Traceback (most recent call last):
File "F:\Extension3.py", line 24, in <module>
print(str("Your average speed for the first journey was "+speed1))
TypeError: Can't convert 'float' object to str implicitly

This is the task:
A person makes three car journeys. For each journey the user has to enter the number of miles travelled and the amount of time spent travelling.
The program will then output the following information for the user.
• Average speed of each individual journey
• Total distance travelled in the three journeys
• Total time spent travelling
• Average distance of a journey
• Average time spent in a journey
• Average speed of a journey

The code I have right now is:
#a=float(input("How many miles did you travel in the first journey? "))
b=float(input("How long did it take? "))
c=float(input("How many miles did you travel in the second journey? "))
d=float(input("How long did that take? "))
e=float(input("And how many miles did the third journey take you? "))
f=float(input("And how long did that take? "))
speed1=a/b
speed2=c/d
speed3=e/f
fulljourney=a+c+e
fulltime=b+d+f
averagedistance=fulljourney/3
averagetime=fulltime/3
averagespeed1=speed1+speed2+speed3
averagespeed=averagespeed1/3
int(speed1)
int(speed2)
int(speed3)
int(fulljourney)
int(fulltime)
int(averagedistance)
int(averagetime)
int(averagespeed)
print(str("Your average speed for the first journey was "+speed1))
print(str("Your average speed for the second journey was "+speed2))
print(str("Your average speed for the third journey was "+speed3))
print(str("All together you travelled "+fulljourney))
print(str("And it took you "+fulltime))
print(str("Your average distance was "+averagedistance))
print(str("Your average time was "+averagetime))
print(str("And your overall average speed was "+averagespeed))

Anyone got any ideas?
2. Change:
print(str("Your average speed for the first journey was "+speed1))

To:
print("Your average speed for the first journey was "+str(speed1))
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
2
Rep Power
0
Thanks man. That worked out
4. object type is so important that when debugging I examine type before value.

Python refuses:
'string '+98

Assuming you want 'string 98', some alternatives:
Code:
```#(recent pythons)
'string {}'.format(98)
'string {0:d}'.format(98)
'string{0:3d}'.format(98)
'string {3:d}'.format('a','bc',2,98,'x')

# all the pythons I've used accept
'string '+str(98)
'string %d'%(98)  # ''%TUPLE deprecated```