简体   繁体   中英

How to print pattern with number of rows input by user

a = ['*','#','$'] b = [1,4,7]

How can I make use of the above lists and loop to print the pattern with the required number of rows? Here are 2 examples if the input is 5 and 10 respectively. Do not need to print the bracket on each line. Thanks.


Number of rows: 5

(*)

(####)

($$$$$$$)

(*)

(####)


Number of rows: 10

(*)

(####)

($$$$$$$)

(*)

(####)

($$$$$$$)

(*)

(####)

($$$$$$$)

(*)

You can use the modulus operator (%) to iterate through a and b and repeat from the beginning of the list.

a = ['*', '#', '$']
b = [1, 4, 7]
rows = int(input('>>> '))
print('\n\n'.join(f'({a[i % len(a)] * b[i % len(b)]})' for i in range(rows)))

You can multiply A* B and then shift A and B by 1 per Loop Iteration

Easy to implement user input

a = ['*','#','$']
b=[1,4,7]
b.reverse()
b = b[-1:] + b[:-1]

print(b)

for x in range(5):
  print(a[0]*b[0])
  a = a[-1:] + a[:-1]
  b = b[-1:] + b[:-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM