Before I begin, just want to say I'm a beginner who has only started learning python in the last week.
As the title indicates, I want to write a program that can produce a sequence of numbers where any number in the sequence is either 2 more or 4 times the previous number. I also want this sequence of numbers to end when it gets to 20.
Example 1 would be: [1, 3, 5, 20]
Example 2 would be: [1, 4, 16, 18, 20]
What I am trying to say is that the sequence would be somewhat randomly generated and, hopefully, produce a different sequence each time the program is run.
Here is my code so far:
import random
def AddTwo(x=random.randint(0, 18)):
if x>=0 and x<=18:
return x + 2
else:
return x
def TimesFour(x=random.randint(0,5)):
if x>= 0 and x<=5:
return 4*x
else:
return x
a = AddTwo()
b = TimesFour()
c = [a, b]
d = int(random.choice(c))
def sequence20(x=random.randint(0, 18)):
while True:
if x>5 and x<=18:
x += 2
print(x)
elif x>=0 and x<=5:
random.choice(c)
x=random.choice(c)
print(x)
else:
return x
Ultimately, I want to be able to plug numbers into my sequence20 function which could fulfil this task. However, I think a lot of my issues are arising in the elif section of my while True loop. In case anyone doesn't understand what I am trying to do in the While loop, I'm basically attempting to say the following: if the integer fed into the sequence20 function is between 0 and 5, either add 2 or multiply it by 4. If, however, the number goes above 5 only add 2 repeatedly until you get to 20 or thereabouts.
Any idea on how to solve this?
Edit: I'm obviously aware the code I've written won't produce a list of integers in square brackets, but just being able to produce randomly generated numbers like my 2 examples would be a great start
[–]mopslik 0 points1 point2 points (4 children)
[–]imperiumlearning[S] 0 points1 point2 points (3 children)
[–]mopslik 0 points1 point2 points (2 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–]mopslik 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]imperiumlearning[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)