Hey there! Sign in to join this conversationNew here? Join for free

how to make this line a integer Watch

Announcements
    • Thread Starter
    Offline

    5
    ReputationRep:
    print(a + b)
    how do i make this line use integers for the calculation? (in python)
    Offline

    7
    ReputationRep:
    print(int(a+b))

    if they are numbers then they should be int already. If they are words/letters then you cant do that.
    • Wiki Support Team
    Offline

    21
    ReputationRep:
    (Original post by Biology123ABC)
    print(a + b)
    how do i make this line use integers for the calculation? (in python)
    I'm not sure what you're asking, could you explain more?
    a = 2
    b = 3
    print (a + b)

    would print 5 to the screen as a and b are integer values.

    (Original post by Agent007)
    print(int(a+b))

    if they are numbers then they should be int already. If they are words/letters then you cant do that.
    You could use + if they were strings or characters.

    a="Hello "
    b="world"
    print (a + b)

    Would print "Hello World"
    Offline

    7
    ReputationRep:
    (Original post by DayneD89)
    I'm not sure what you're asking, could you explain more?
    a = 2
    b = 3
    print (a + b)

    would print 5 to the screen as a and b are integer values.



    You could use + if they were strings or characters.

    a="Hello "
    b="world"
    print (a + b)

    Would print "Hello World"
    what i meant was if a and b were strings (eg, str(3) ad str(2)) then he would not be able to add them. I wasn't referring to concatenation.
    • Wiki Support Team
    Offline

    21
    ReputationRep:
    (Original post by Agent007)
    what i meant was if a and b were strings (eg, str(3) ad str(2)) then he would not be able to add them. I wasn't referring to concatenation.
    In a way, you could, though there would be little point.

    a=ord('a')
    b=ord('b')
    print (a+b)


    would print 195 (ord returns the asci value of a character)
    • Thread Starter
    Offline

    5
    ReputationRep:
    (Original post by DayneD89)
    I'm not sure what you're asking, could you explain more?
    a = 2
    b = 3
    print (a + b)

    would print 5 to the screen as a and b are integer values.

    for me if I do print(a + b) when a = 5 and b = 22 it comes up as 522 and would you know what code to use if I wanted to get any number divided by 2 until the answer reached 0? thanks
    Offline

    7
    ReputationRep:
    (Original post by DayneD89)
    In a way, you could, though there would be little point.

    a=ord('a'
    b=ord('b'
    print (a+b)


    would print 195 (ord returns the asci value of a character)
    but 3+2 is not 195. Ur honestly taking this to new unnecessary levels.
    • Wiki Support Team
    Offline

    21
    ReputationRep:
    (Original post by Biology123ABC)
    for me if I do print(a + b) when a = 5 and b = 22 it comes up as 522 and would you know what code to use if I wanted to get any number divided by 2 until the answer reached 0? thanks
    When you initialize a and b are you using quotes like this-

    a="5"
    ?

    The quote marks would make it a string, but
    a=5
    should make a an int.

    a/b would give a divided by b, but what do you mean by until the answer reaches 0?
    • Wiki Support Team
    Offline

    21
    ReputationRep:
    (Original post by Agent007)
    but 3+2 is not 195. Ur honestly taking this to new unnecessary levels.
    Sorry, you've lost me. The 195 is 97 ('a' in asci) + ('b' in asci). I'm just pointing out that you kinda can add two characters together, though as I say, pointless. Concatenation is essentially the same thing as addition for strings.
    • Thread Starter
    Offline

    5
    ReputationRep:
    (Original post by DayneD89)
    When you initialize a and b are you using quotes like this-

    a="5"
    ?

    The quote marks would make it a string, but
    a=5
    should make a an int.

    a/b would give a divided by b, but what do you mean by until the answer reaches 0?
    say you had 122, is there a way to make python code do this:
    122/2 = 61 r 0
    61/2 = 30 r 1
    30/2 = 15 r 0
    15/2 = 7 r 1
    7/2 = 3 r 1
    3/2 = 1 r 1
    1/2 = 0 r 1
    0/2 = 0 r 0
    I'm trying to make code that converts denary to binary, or if there is an easier way to do it then what is it? :/
    Offline

    7
    ReputationRep:
    (Original post by Biology123ABC)
    say you had 122, is there a way to make python code do this:
    122/2 = 61 r 0
    61/2 = 30 r 1
    30/2 = 15 r 0
    15/2 = 7 r 1
    7/2 = 3 r 1
    3/2 = 1 r 1
    1/2 = 0 r 1
    0/2 = 0 r 0
    I'm trying to make code that converts denary to binary, or if there is an easier way to do it then what is it? :/
    easiest way is to use recursion

    def convertToBinary(n):
    """Function to print binary number for the input decimal using recursion"""
    if n > 1:
    convertToBinary(n//2)
    print(n % 2,end = '')
    # decimal number
    dec = 34
    convertToBinary(dec)
    100010
    • Thread Starter
    Offline

    5
    ReputationRep:
    (Original post by Agent007)
    easiest way is to use recursion

    def convertToBinary(n):
    """Function to print binary number for the input decimal using recursion"""
    if n > 1:
    convertToBinary(n//2)
    print(n % 2,end = ''
    # decimal number
    dec = 34
    convertToBinary(dec)
    100010
    i have no idea what recursion is ._.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Will you be richer or poorer than your parents?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.