The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Newbie question
Discuss Newbie question in the Python Programming forum on Dev Shed. Newbie question Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 26th, 2013, 09:55 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 42 m 36 sec
Reputation Power: 0
|
|
|
Newbie question
def func1(a,b,c):
e = a + b/c
return formatted(e)
def func2(f,g,h,i):
j = func1(f,g,h) + i
return formatted(k)
def func3(f,g,h,i):
j = (f + g/h) + i
return formatted(j)
def formatted(a):
return ("$%.2f" % a)
When I run the below....
print(func1(1,2,3))
print(func2(1,2,3,4))
print(func3(1,2,3,2))
I get
Traceback
<module> D:\Python33\test.py 18
func2 D:\Python33\test.py 6
TypeError: Can't convert 'int' object to str implicitly
If I run this
print(func1(1,2,3))
print(func3(1,2,3,2))
it runs fine..
>>>
$1.67
$3.67
What Am I doing wrong, I need the func2 format to work especially with float as the last parameter.
Thanks for looking.
|

January 27th, 2013, 11:06 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 75
  
Time spent in forums: 1 Day 3 h 5 m 57 sec
Reputation Power: 2
|
|
|
You can't add a float to a string.
Also, your func2 references a "k" variable does doesn't exist.
|

January 27th, 2013, 11:09 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 42 m 36 sec
Reputation Power: 0
|
|
Hi I figured this out. I will post the solution in case it may help someone.
The return value from func1 is a string. You can not add float or int to a string. The return has to be converted to a float.
def func1(a,b,c):
e = a + b/c
return formatted(e)
def func2(f,g,h,i):
Code:
Original
- Code |
|
|
|
j = float(func1(f,g,h)) + i
return formatted(j)
def func3(f,g,h,i):
j = (f + g/h) + i
return formatted(j)
def formatted(a):
return ("%.2f" % a)
print(func1(1,2,3))
print(func2(2,3,4,4))
print(func3(1,2,3,2))
This work fine.
|

January 27th, 2013, 11:12 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 42 m 36 sec
Reputation Power: 0
|
|
|
Thanks sir, I think we were writing the answer at the same time.
|

January 27th, 2013, 11:13 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 42 m 36 sec
Reputation Power: 0
|
|
|
Could you please tell me, how can I format the output like in my original post
Thanks
|

January 27th, 2013, 01:19 PM
|
 |
Contributing User
|
|
|
|
Your original function contained a $ character.
def formatted(a): return ("$%.2f" % a)
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|