0

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?

  • 3
    Use integer division `//` instead of `/` – rdas Nov 14 '21 at 13:53
  • Does this answer your question? [Why does Python integer division yield a float instead of another integer?](https://stackoverflow.com/questions/1282945/why-does-python-integer-division-yield-a-float-instead-of-another-integer) – mkrieger1 Nov 14 '21 at 14:00

6 Answers6

2

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)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
2

You can use convertation from float to int:

num = 123.0
print(int(num))

Output:

123
FSTMAX
  • 76
  • 1
  • 7
1

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)
Junaid
  • 159
  • 1
  • 15
1

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().

The Reider
  • 143
  • 6
1

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

Bip Lob
  • 485
  • 2
  • 6
  • 15
1

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)

Bernana
  • 245
  • 1
  • 12