Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 08:05 AM
Crazyfrogv2 Crazyfrogv2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 Crazyfrogv2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

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?

Reply With Quote
  #2  
Old November 15th, 2012, 08:14 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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))

Reply With Quote
  #3  
Old November 15th, 2012, 08:18 AM
Crazyfrogv2 Crazyfrogv2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 Crazyfrogv2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 6 sec
Reputation Power: 0
Thanks man. That worked out

Reply With Quote
  #4  
Old November 15th, 2012, 08:26 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Float, string and integer

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap