Python Forum

Full Version: simple code task
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
Hello, what have you tried? Show the code of your attempt at solving this, and we can help correct the mistakes.
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:
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