Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape indentation
#1
Hello,

Its probably the wrong way to do this, but i am trying to print my list and because of the if statement to get rid of the false returned, i ended up with doubles in my list.

prime = []
def is_prime_v1(n):
    
    if n == 1:
     return False
    
    for d in range(2, n):
        if n % d == 0:
            return False
    prime.append(n)
    return n
  
for n in range(1,21):
  if is_prime_v1(n) != False:
    print(is_prime_v1(n))
   
print(prime)  
Output:
2 3 5 7 11 13 17 19 [2, 2, 3, 3, 5, 5, 7, 7, 11, 11, 13, 13, 17, 17, 19, 19] >
print(prime) seems to be prisonner of the for indent. It cause the numbers to repeat twice.
How to avoid this?
Of course the Sieve of Eratosthenes is more efficent. The question is just about controling the return and indentation.

Thank you
Reply


Messages In This Thread
Escape indentation - by Frankduc - Jan-28-2022, 02:25 PM
RE: Escape indentation - by BashBedlam - Jan-28-2022, 02:53 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 03:05 PM
RE: Escape indentation - by snippsat - Jan-28-2022, 03:13 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 03:36 PM
RE: Escape indentation - by Gribouillis - Jan-28-2022, 03:41 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 04:07 PM
RE: Escape indentation - by deanhystad - Jan-28-2022, 04:24 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 04:50 PM
RE: Escape indentation - by deanhystad - Jan-28-2022, 07:24 PM
RE: Escape indentation - by Gribouillis - Jan-28-2022, 09:59 PM
RE: Escape indentation - by Frankduc - Jan-31-2022, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 938 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  add Escape charcters in string GrahamL 3 1,210 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,865 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,346 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,054 Dec-23-2020, 06:47 PM
Last Post: Mark17
  help for escape sequences NewPi 1 2,065 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  escape single quote deep_logic 1 1,827 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  The use of escape char \ hishamzero1 2 2,397 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  Escape sequences display in python Uchikago 1 2,458 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Python 3 escape codes oldDog 1 2,616 Sep-15-2018, 10:12 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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