Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
November 15th, 2012, 08:05 AM
 Crazyfrogv2
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 21 m 6 sec
Reputation 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

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("And your overall average speed was "+averagespeed))

Anyone got any ideas?

#2
November 15th, 2012, 08:14 AM
 rrashkin
Contributing User

Join Date: May 2012
Location: 39N 104.28W
Posts: 90
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 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
November 15th, 2012, 08:18 AM
 Crazyfrogv2
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 21 m 6 sec
Reputation Power: 0
Thanks man. That worked out

#4
November 15th, 2012, 08:26 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,350
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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```
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Float, string and integer