Turn on thread page Beta
 You are Here: Home

# how to make this line a integer watch

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

if they are numbers then they should be int already. If they are words/letters then you cant do that.
3. (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"
4. (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.
5. (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)
6. (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
7. (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.
8. (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?
9. (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.
10. (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? :/
11. (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
12. (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 ._.

Reply
Submit reply
Turn on thread page Beta

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: October 21, 2017
Today on TSR

### Predict your A-level results

How do you think you'll do?

### University open days

1. University of Bradford
University-wide Postgraduate
Wed, 25 Jul '18
2. University of Buckingham
Psychology Taster Tutorial Undergraduate
Wed, 25 Jul '18
3. Bournemouth University
Clearing Campus Visit Undergraduate
Wed, 1 Aug '18
Poll
Useful resources

## Make your revision easier

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups

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

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