Reputation: 45
I have an integer in my code and want to connect it with other integers.
str1 = ''
int1 = 4
int2 = 16
int3 = 32
int4 = 64
int5 = int4 / int3
int6 = int2 / int1
str1 += str(int5)
str1 += str(int6)
print(str1)
now the output would be 2.04.0, but i want it to be 24. How do i remove the .0?
Upvotes: 0
Views: 796
Reputation: 56
You can use conversion from float to int:
num = 123.0
print(int(num))
Output:
123
Upvotes: 2
Reputation: 247
As @rdas commented you can use integer division (// instead of /) and you will get integer answer (6)
or you can just the 2 numbers together like:
str1 += str(int5 + int6)
And you will get float answer (6.0)
Upvotes: 1
Reputation: 731
You can use integer division using '//' instead of '/' otherwise you can use type casting like int5 = int(int4/int3)
. Both of this will solve your problem
Upvotes: 1
Reputation: 163
Here is the fix.
str1 = ''
int1 = 4
int2 = 16
int3 = 32
int4 = 64
int5 = int(int4 / int3)
int6 = int(int2 / int1)
str1 += str(int5)
str1 += str(int6)
print(str1)
If you divide than the Output will be a float (not an int). So, if you convert int5 and int6 or (like i did) convert the calculation result in the calculation to a int. The main methode you use to do it is int()
.
Upvotes: 1
Reputation: 24049
You need to get values as int
so you need to change these two lines:
int5 = int4 // int3
int6 = int2 // int1
Then you can use f-string
:
print(f'{int5}{int6}')
Or use your own code:
str1 += str(int5)
str1 += str(int6)
print(str1)
Upvotes: 2
Reputation: 149
Cast int5
and int6
to an integer.
str1 = ''
int1 = 4
int2 = 16
int3 = 32
int4 = 64
int5 = int(int4 / int3)
int6 = int(int2 / int1)
str1 += str(int5)
str1 += str(int6)
print(str1)
Upvotes: 1