#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Convert number into text


    Hi there!

    I'm completely new to programming and I've been playing around with Python. I was able to code a very simple calculator but I got stuck on an error message that came up after I tried to add additional code. This is the code:

    Code:
    print("Hello, welcome to this calculator game")
    number = int(input("Please choose a number: "))
    number2 = int(input("Now please another number: "))
    print("Good, now tell me you want me to do with those numbers")
    print("The options are:")
    print("sum")
    print("substract")
    print("divide")
    print("multiply")
    choice = input("Type in here the what you want to do: ")
    if choice == "sum":
        print (("The result is " (number+number2)))
    else:
        if choice == "substract":
             print("The result is "(number-number2))
            
        else:
            if choice == "divide":
                  print("The result is "(number/number2))
                
            else:
                if choice == "multiply":
                     print("The result is "(number*number2))
                      
                else:
                     print("that's not an option")
    This was working earlier since I was only printing the results, but then I thought I'd add something like "the result is (result)" and that's when the can of worms opened. When I run the code and do a sum, I get this error:

    Traceback (most recent call last):
    File "C:/Users/renato/Desktop/HTML Files/Python/calculator.py", line 12, in <module>
    print (("The result is " (number+number2)))
    TypeError: 'str' object is not callable


    I did some research on 'str' and I tried to fix the code by doing the following:

    print
    Code:
    str( (("The result is " (number+number2)))
    that gave me a syntax error so I tried print
    Code:
    (("The result is " str((number+number2))))
    and that also results in a syntax error.

    What am I missing here?

    I appreciate the help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Numbers are converted to string - this is fine. But the strings are not concatenated. Use + for string concatenation.

    Code:
    print ("The result is " + str(num1+num2))
    By the way, I couldn't help notice, you may choose to use string literals instead of calling the print function each time one after the other.

    Code:
    print('''Good, now tell me you want me to do with those numbers
    The options are:
    sum
    substract
    divide
    multiply''')
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    got it


    Hi noobie1000,

    that fixed it! Thank you so much!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    else if statement in python


    Python supports elif . Use elif to avoid the deeply indented if else if blocks.
    Code:
    message = '''
    Good, now tell me you want me to do with those numbers
    The options are:
        sum
        substract
        divide
        multiply
    Type in here the what you want to do:
    '''[1:-1]+' '
    
    print("Hello, welcome to this calculator game")
    number = int(input("Please choose a number: "))
    number2 = int(input("Now please another number: "))
    choice = input(message)
    
    if choice == "sum":
        result = number+number2
    elif choice == "substract":
        result = number-number2
    elif choice == "divide":
        result = number/number2
    elif choice == "multiply":
        result = number*number2
    else:
        result = None
    
    if result is None:
        print("that's not an option")
    else:
        print("The result is {}".format(result))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    thanks


    Hi,

    Thanks for the tip! I will give elif a try too.

    Thanks for the feedback!
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Since you're listening, we also don't need case statements. if then elif elif elif else is a case statement. Use a dictionary instead. Anyway, it's a common alternative. You may decide it's easier to create new choices. In this version you can include a power option simply by updating the dictionary.
    Code:
    import operator
    import math
    
    operations = dict(
        sum = operator.add,
        subtract = operator.sub,
        divide = operator.truediv,
        multiply = operator.mul,
        log = math.log,
        )
    
    message = ('''
    Good, now tell me you want me to do with those numbers
    The options are:
        {}
    Type in here the what you want to do:
    '''[1:-1]+' ').format('\n    '.join(operations.keys()))
    
    print("Hello, welcome to this calculator game")
    number = int(input("Please choose a number: "))
    number2 = int(input("Now please another number: "))
    choice = input(message)
    
    if choice in operations:
        print("The result is {}".format(dyad(number, number2)))
    else:
        print("that's not an option")
    Last edited by b49P23TIvg; June 7th, 2013 at 10:41 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    thanks


    Thanks.

    I think that's a bit advanced for me, I just started to learn programming/python last week, but I'm sure I will remember this in the future.

    I'm familiar with case statements since we use them on formula fields in Salesforce (I'm a Salesforce admin) but didn't know python had those too.

    Again, thanks for the tips. Really appreciate it
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    python doesn't have a case statement. Use either the if elif structure or the preferred dictionary.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    :)


    got it thanks!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Awesome


    So I used both tips (str & elif) and this is the result, it's working as expected

    Code:
    number = int(input("Give me a number: "))
    number2 = int(input("Give me one more: "))
    print('''Tell me what you want to do, the options are:
    sum
    substract
    divide
    multiply''')
    choice = input("what will you do? ")
    if choice == "sum":
        print("The result is " +str(number+number2))
    elif choice == "substract":
        print("The result is " + str(number-number2))
    elif choice == "divide":
        print("The result is " + str(number/number2))
    elif choice == "mutiply":
        print("The result is " + str(number*number2))
    else:
        print("That's not an option")
    Thank you very much for all your help.

IMN logo majestic logo threadwatch logo seochat tools logo