The Student Room Group

how to make this line a integer

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

if they are numbers then they should be int already. If they are words/letters then you cant do that.
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"
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.
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)
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
Original post by DayneD89
In a way, you could, though there would be little point.

a=ord('a' :wink:
b=ord('b' :wink:
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.
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?
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.
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? :/
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
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 = '':wink:
# decimal number
dec = 34
convertToBinary(dec)
100010


i have no idea what recursion is ._.

Quick Reply

Latest