Can someone please help me with this because it is driving me crazy.
I wrote a program which prompts the user to enter a number
I convert the string to an integer
when i print the value it sometimes comes out as a decimal e.g. 3.0
I thought an integer was a whole number so how is it printing as a decimal?

Need to see your code.

Here is my code. I don't understand why at first it is an integer and then it goes to a decimal. I already convertied it to a decimal during the input. The first number prints out as an integer and then all the numbers that follow have .0 after the number

x=int(input("enter a number"))

while x > 1:
    if x % 2==0:
        x = x/2

    else:
        x = 3*x+1
    print (x)

When i print x does that convert it back to a string?

3/2 will be 1.5
3//2 will be 1

yes but the answer of 1.5 would not be an integer. It should come out as 1 unless i round it surely?

Use the floor divider // instead. The typing system of Python will change your int to a double, if the / operator does not divide exactly. Only Vegaseat can tell if what I say here is wrong!

Well it worked! but i just still don't understand why!

Google something about integer division in computer languages.
In Python // could be called integer division and / double division.
Closely related is the modulo operator %

Oh right ok i have looked into it and i understand now the difference between / and //.
10/3 would give 3.33333
and 10//3 would give 3.
when i use / does that automatically change it from an int to a different type?
Sorry if i am being frustrating i just want to understand how it works

Yes, ddanbe is correct. In Python version 3 division using / gives you a float and division by // gives you an integer. You make sure you have an integer you can use int() to change a float into an integer.

Some tests with Python3 ...

x = 6//3
print(x, type(x))  # 2 <class 'int'>

x = 6/3
print(x, type(x))  # 2.0 <class 'float'>

x = int(6/3)
print(x, type(x))  # 2 <class 'int'>

print(int(1.5))  # 1

The Problem with Integer Division
Not much else to say if you read that post.
One tips is to use from __future__ import division for Python 2.x.
Python 3.x has made chage to integer-division.

#Python 2.7
>>> 10 / 3
3
>>> from __future__ import division
>>> 10 / 3
3.3333333333333335

--

#Python 3.3
>>> 10 / 3
3.3333333333333335
commented: good link +14

IMO, you ought to always just do "from future import division" or make sure that you are working soley with ints. The only time I think it would be necessary to maintain ints when possible is if you're doing very intense computations and you can't afford the extra memory it takes to store a float.

There are a number of functions that need integer arguments.

You are using Python 3x. Back in the Python 2x day the "/" (single slash) operator worked similarly to C/C++ and Java. That is, if the numerator and denominator are integers the result is integer (truncating the fractional part). Thus 3/2 is 1, not 1.5. With Python 3x that changed. The single-slash operator always give a float result. Thus, 3/2 is 1.5. In Python 2x the "//" was introduced as a prelude of things to come in Python 3x today, where "//" is integer division and "/" is float division.

In truth, "//" in Python 3x is more complicated than integer division. Think of N//M meaning "number of times M goes into N" and futuremore, if either N or M are float the result is float. Thus 80//35 is 2, but 8//3.5 is 2.0.

When Python moved from version 2x to 3x it created lots of heartburn for programmers who expected languages to evolve in a backward-compatible way. But Python 2x programs don't run under Python 3x systems and can give different results. The change in how "/" works is an example of one of these changes. So if you learned to program in C/C++, Java, and most other programming languages (even FORTRAN!), then this could be a source of confusion for you.

Hope that helps explain things.

Ah thank you everyone! Appreciate the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.