Python Forum
number repeating twice in loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number repeating twice in loop
#1
Hi,
can you help me understand why the number 9 repeats twice when i run this loop?

import math

digits = 123456789
reversed_numbers = []
counter = 1
for digit in range(9):
    test = (digits / (10 * counter)) % 10
    counter *= 10
    after_point = test - int(test)
    digit_after_point = int(10 * round(after_point, 1))
    print(digit_after_point)
Reply
#2
Hello,

The problem as it seems is when you use the int() function on the expression: "10 * round(after_point, 1)".

Executing the two first step of your loop, the value of "after_point" variable are so close: 0.90000 for step 1 and 0.88999 for step 2. When rounding, it will give the same value in the either cases (0.9).
Reply
#3
Thank you
Reply
#4
for digit in range(9):
You are aware that digit iterates from 0 to 8, so 9 is not included.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 692 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,264 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,492 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,239 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,021 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,791 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,894 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  Repeating equations Tbot100 2 3,263 May-29-2019, 02:38 AM
Last Post: heiner55
  First non-repeating char and some errors. blackknite 1 2,268 Jan-06-2019, 02:19 PM
Last Post: stullis
  repeating for loop Kaldesyvon 5 3,839 Dec-06-2018, 08:00 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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