7

Here is my code so far:

def main():
    for var in range (1, 101):
        num= IsPrime(var)
        if num == 'true':
             print(var, end=', ') 

The IsPrime function calculates whether or not a function is prime.

I need to print out the prime numbers from 1 to 100 formatted into a single line with commas and spaces in between. for example, output should look like:

2,  3,  5,  7,  11,  13,  17,  19,  23,  29,  31,  37,  41,  43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97 

I tried to run my program, but I always get a trailing comma at the end of 97. I don't know how to remove the comma, and because it is a loop, str.rstrip and [:-1] don't work.

I need to use a loop and I can't use

print('2') 
print(', ', var, end='') 

for the other prime numbers.

I can't tell if there's an easier way to code this or I'm not aware of a function that can do this correctly.

1
  • IsPrime(var) should be returning a boolean not a string then if IsPrime(var) would be sufficient to check Commented Jul 19, 2015 at 23:52

2 Answers 2

10

The idiomatic Python code in my opinion would look something like this:

print(', '.join([str(x) for x in xrange(1, 101) if IsPrime(x) == 'true']))

(Things would be better if IsPrime actually returned True or False instead of a string)

This is functional instead of imperative code.

If you want imperative code, you should print the ', ' before each element excepting the first item of the loop. You can do this with a boolean variable which you set to true after you've seen one item.

Sign up to request clarification or add additional context in comments.

8 Comments

Would be even more pythonic, if you deleted the [ ... ] around the expression. Then it would just be a light-weight generator that is passed to the join-method...
@koffein Not bad at all! I like filter myself, as it works in pretty much any functional programming language.
Well, in this case ifilter from itertools would be a function to consider, because as far as I know filter returns a list, just like the list comprehension with [ ... ] up there. I personally do not like filter, if I have to create a lambda inline. I a function already exists, its fine, but otherwise it looks more crowded to me then the list-comprehension/generator...
@koffein, that would actually be worse as python then still has to build a list for join.
Am I wrong when I say that print(', '.join([x for x in xrange(1, 101) if IsPrime(x) == 'true'])) fails at all, as join() expects a string but gets an int? So it misses an str(x), doesn't it?
|
5

You can put all the numbers into a list and then join all the values:

def main():
    primes = []
    for var in range (1, 101):
        if IsPrime(var) == 'true':
            primes.append(var)
        num = IsPrime(var)
    print(', '.join(primes))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.