-2

need to create a program that generates a random number between and including 5-10 and then forms the following pattern.

"The random number generated is" 9
9 8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1 
1

Here is the code I have used:

from random import choice
m=choice(range(5,11))
n=m-1
o=n-1
p=o-1
q=p-1
r=q-1
s=r-1
t=s-1
u=t-1
v=u-1
print("The random number generated is",m)
while m>0:
    print(m,n,o,p,q,r,s,t,u,v)
    m=m-1 
    n=n-1
    o=o-1
    p=p-1
    q=q-1
    r=r-1
    s=s-1
    t=t-1
    u=u-1
    v=v-1

However I get this when running:

The random number generated is 8
8 7 6 5 4 3 2 1 0 -1
7 6 5 4 3 2 1 0 -1 -2
6 5 4 3 2 1 0 -1 -2 -3
5 4 3 2 1 0 -1 -2 -3 -4
4 3 2 1 0 -1 -2 -3 -4 -5
3 2 1 0 -1 -2 -3 -4 -5 -6
2 1 0 -1 -2 -3 -4 -5 -6 -7
1 0 -1 -2 -3 -4 -5 -6 -7 -8

How do I get rid of the 0 and negatives? Or a better set of code?

Thanks

  • 1
    Possible duplicate of [Numerical patterns in Python3](http://stackoverflow.com/questions/42608684/numerical-patterns-in-python3) – Juan T Mar 05 '17 at 17:38
  • I'd suggest you create an array instead of 752 variables. You'll then be able to make loops & tests really easily. – Jean-François Fabre Mar 05 '17 at 17:40

3 Answers3

1

It would be much easier to do with a few loops instead of setting all those variables -- take a look at below for one option:

from random import choice

# max_start is the maximum number we start with
max_start = choice(range(5, 11))

print("Starting number: {0}".format(max_start))

# We iterate downward
for start in range(max_start, 0, -1):

  # And for each line, start from that point down to 1
  for n in range(start, 0, -1):

    # And print it, no end of line character
    print("{0} ".format(n), end='')

  # And add a newline
  print()

Output:

Starting number: 5
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 
Scovetta
  • 3,112
  • 1
  • 14
  • 13
1

The problem is that with your while loop will only stop when your m is zero but will keep printing the same amount of numbers. It would be better to use a while and a for loop. For example:

 import random
 i = random.randint(5,10)
 while i>0:
     for j in range(i,0,-1):
         print(j,end=" ")
     print("")
     i -= 1
nomis6432
  • 236
  • 2
  • 10
0

Whenever you find youself making up a list of variables like m, n, o, p, ... there is a good chance that you should use a list instead. The range function can generate assending or desending integers that you can use to make the list. After that, its just a question of printing and removing values from the list.

import random

# generate a random number from 5 to 10 inclusive
num = random.randint(5, 10)
print("The random number generated is", num)

# make a list, counting down from the number, excluding 0, converting
# to string
numbers = list(str(n) for n in range(num, 0, -1))

# print and trim the number list til there is nothing left
while numbers:
    print(' '.join(numbers))
    numbers.pop(0)
tdelaney
  • 73,364
  • 6
  • 83
  • 116