Generating a list that is then used in further functions


#1

Hi all,

I’m VERY new to python; I’m trying to learn to code to do some maths work. So I’m looking to understand how to write certain functions/programs based around maths.

What I’d like to do is to start with a range of numbers the user specifies. Also, I’d like a list of just a single number to begin with. Then I’d like to run a test against each number in the original range, and each time, if certain conditions are met, the number from the range is added to the list of the (originally) single number. Then the next number from the range is tried against both numbers in the list. If it passes, it is also added to the list. If not, its discarded.

I hope that makes sense. Any help would be gratefully received. Once I figure this all out, I should be able to build on the principles for later.

Many thanks

Bob


#2

the first question is, does the final list constructed by the user persist after the program finishes running?

Such a program is relatively simple to make. We can help you, but you have to write it

What do you have so far? What python version are you going to run your program in? For new projects python3 is recommended


#3

Thanks for the reply.

Basically, I want to generate a list of prime numbers within a given range, and then it outputs to a growing list of primes that are then used to test against the next number in the range.

Eg. test 3-10. The list starts with just 2. 3 passes, so it is added to the list. So then 4 is tested against both 2 and 3 and fails, so is discarded. 5 is then tested, passes, so joins 2 and 3 in the list.

Eventually, I’d like to output the list as a text file, if that is possible?


#4

one of the challenge at some point in the python course is to write a is_prime program (simple implementation), so you can work on the python course? Which also features reading and writing to text files (near the end)

so yes, this is certainly possible. But good prime number finder algorithms already exist, if you really want to have an effective system, you should not re-invent the wheel and use an existing algorithm.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.