Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple code task
#1
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.
Reply
#2
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
Hello, what have you tried? Show the code of your attempt at solving this, and we can help correct the mistakes.
Reply
#4
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:
Reply
#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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 226 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 439 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 869 Nov-12-2022, 07:52 AM
Last Post: buran
  count certain task in task manager[solved] kucingkembar 2 1,083 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  Simple encoding code ebolisa 3 1,399 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,750 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,237 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,576 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,749 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 2,992 Jun-16-2021, 02:06 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020