Python Forum
simple code task - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: simple code task (/thread-10919.html)



simple code task - dan123445 - Jun-13-2018

hi ,
i need some help,
how can i print in python all the numbers from 0 to 5 in 0.1 steps , but the numbers (1.0,2.0,3.0,4.0,5.0) will be printed at int type.
like that:
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
.
.
thanks ahead.


RE: simple code task - ljmetzger - Jun-13-2018

That's not the way this forum works. Show us the code you have tried, and we will point you in the right direction.

Take a look at .format:
x = 3
print("{:.1}".format(x))
Lewis


RE: simple code task - j.crater - Jun-13-2018

Hello, what have you tried? Show the code of your attempt at solving this, and we can help correct the mistakes.


RE: simple code task - dan123445 - Jun-13-2018

i = 0
while i < 5:
if i in range(0,6):
print int(i)
i = i + 0.1
else:
print i
i = i +0.1

-ive tried also :
if i == 0 or i == 1 or i==2 or i ==3 or i == 4 or i == 5:


RE: simple code task - ljmetzger - Jun-13-2018

Please read the forum rules and insert your code in CODE TAGS. CODE TAGS preserve indentation, which is very important in Python. https://python-forum.io/misc.php?action=help&hid=25

Here are three possible methods. For loops are considered more pythonic than while loops.
myvalue = 0
while myvalue <= 50:
    print("{:.1}".format(myvalue/10 ))
    myvalue += 1
    
# Possible extra value printed due to rounding error
myvalue = 0.0
while myvalue <= 5.0:
    print("{:.1}".format(myvalue))
    myvalue += 0.1
    
for myvalue in range(0, 51):
    print("{:.1}".format(myvalue/10 ))
Lewis