-1

One part of my program displays n amount of prime numbers depending on the user input but no matter what i input it only prints "1"

def listPrimeNumbers():
    print("List Prime Numbers")
    print("------------------")
    print("Enter how many prime numbers you want displayed")
    print("Type in '0' to go back to the Main Menu")
    print("\n"*10)

    amountOfNumbers = int(input("Amount of Numbers --> "))

    print("\n"*10)
    for i in range(1, amountOfNumbers):
        prime = True
        for i in range(2,i):
            if (num%i==0):
                prime = False
        if prime:
            print(i)

    print("\n"*10)
    print("Type '0' to try again and '1' to go to the main menu")
    print("\n"*10)

    choice = int(input("Choice ---> "))

    if choice == 0:
        print("\n"*100)
        listPrimeNumbers()
    elif choice == 1:
        print("\n"*100)
        main()

2 Answers2

1

Change your 'for' loop to this:

    for num in range(1, amountOfNumbers):
    prime = True
    for i in range(2,num):
        if (num%i==0):
            prime = False
    if prime:
        print(num)

num wasn't defined anywhere in your code, I think this is what you meant.

How are you calling your code? As is, I couldn't run it. I managed to run it by adding a call to function listPrimeNumbers() at the bottom.

Anyway, assuming the code you've posted is your entire code, this is the version that worked for me:

import sys

def listPrimeNumbers():
    print("List Prime Numbers")
    print("------------------")
    print("Enter how many prime numbers you want displayed")
    print("Type in '0' to go back to the Main Menu")
    print("\n"*10)

    amountOfNumbers = int(input("Amount of Numbers --> "))

    print("\n"*10)
    for num in range(1, amountOfNumbers):
        prime = True
        for i in range(2,num):
            if (num%i==0):
                prime = False
        if prime:
            print(num)

    print("\n"*10)
    print("Type '0' to try again and '1' to go to the main menu")
    print("\n"*10)

    choice = int(input("Choice ---> "))

    if choice == 0:
        print("\n"*100)
        listPrimeNumbers()
    elif choice == 1:
        print("\n"*100)
        print 'Bye'
        sys.exit(0)


listPrimeNumbers()
Alex Dubrovsky
  • 240
  • 4
  • 14
  • Note that for efficiency you only need to go up to `sqrt(num)`; and you should break as soon as prime is false. – Daniel Roseman Feb 01 '18 at 08:51
  • You are correct, but first he has to make sure his code actually runs and does what he intended it to do :) I think he wants to print all prime numbers in a given range. – Alex Dubrovsky Feb 01 '18 at 08:54
  • This works better than mine however when i type in 100 i get all the primes up to 100 while i want to get the first 100 primes, in my code my thinking got mixed up and this is what it would have done if it worked – Thomas Voss Feb 01 '18 at 09:36
  • Then Gianlucca's answer below is what you actually wanted to do. – Alex Dubrovsky Feb 01 '18 at 09:40
0

If I understand correctly the question, you have a logic flaw in the code since, even with the correction Alex suggested (which works), you just print all the numbers that are prime up to the value the user input and not, as seems to be the question, the quantity the user imput.

For example, given your question, if the user input 10, I understant that you should print the first 10 prime numbers (1,2,3,5,7,11,13,17,19,23) and not the primes up to 10 (1,2,3,5,7)

If my assumption is right, the code should be something like:

import math

def listPrimeNumbers(n):
    l = int(math.sqrt(n))+1
    if n == 1:
        return True
    for x in range(2, l):
        if (n%x==0):
            return False
    return True

print("List Prime Numbers")
print("------------------")
print("Enter how many prime numbers you want displayed")
print("Type in '0' to go back to the Main Menu")
print("\n"*10)

amountOfNumbers = int(input("Amount of Numbers --> "))

counter = 0
n = 0
while (counter < amountOfNumbers):
    n += 1
    if listPrimeNumbers(n) == True:
        counter += 1
        print(n)

I omitted the part to reiterate the process, so you need to re-run the program to give another try

Gianluca
  • 3,227
  • 2
  • 34
  • 35