Having trouble with multiplying a variable with a number

MasterDewie picture MasterDewie · May 12, 2011 · Viewed 42.7k times · Source

I'm trying to shorten my code and have more functionality but its not working right.

Heres my code(basically)

def times4():  
    number = droid.dialogGetInput('Input', 'Enter a number between 1 and 10').result  
    number4 = number * 4  
    if number == '1':  
        droid.dialogCreateAlert(number,number + ' * 4 =' + number4)  
        droid.dialogSetPositiveButtonText('Ok')  
        droid.dialogShow()  
        droid.dialogGetResponse()  

And I get this:

1,1 * 4 = 1111

When I want to get this:

1,1 * 4 = 4

Answer

bluepnume picture bluepnume · May 12, 2011

You want:

number = int(number)

At the moment, 'number' is a string (i.e. '1' rather than 1) so multiplying it by four naturally just gives you '1111'. Using int() will give you an integer representation of the string.