2

Here is the code ...

a=4
b=8.0
if a and a >0:
 a=a*int(b)
 print "Value:",a 

The desired o/p should be 32. i am also getting the same in python console. But the same code is present in my product where instead of 32 the out put is coming as 44444444 (eight fours) i.e whatever value i'am giving to multiply with "a" it is just printing that much a's. If you give 12 then twelve fours will print.

Any idea what is the problem ?

asked Mar 8, 2012 at 11:58
1
  • 4
    The code is not exactly as such in your "product". Show the part where you "give a value" to multiply. Commented Mar 8, 2012 at 12:01

3 Answers 3

10

I bet you 100 bob that in your product a is not actually the integer 4, but the string '4'.

>>> a = '4'
>>> b=8.0
>>> if a and a >0:
... a=a*int(b)
... print "Value:",a
... 
Value: 44444444

This will happen, for example, if you are using something like a = raw_input('Please enter a number: ') and then you forget to convert the string a into a number.

answered Mar 8, 2012 at 12:01
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Wim, correct! the value it is fetching through some function whose default return value is String. Uhh sily mistake !!!
6

'a' must be a string in your code:

>>> a = 4
>>> b = 8
>>> a * b
32
>>> a = "4"
>>> a * b
'44444444'
>>> int(a) * b
32
answered Mar 8, 2012 at 12:02

1 Comment

Thanks Vinod, correct! the value it is fetching through some function whose default return value is String. Uhh silly mistake !!!
4

That looks like a is a string, and not an integer. Try this:

a = int(a) * int(b)

Hope that helps

answered Mar 8, 2012 at 12:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.