0

I'm trying to write a program were a user enters a list, the program reads through a list and rotates the numbers in the list one position for each interaction until the numbers reach starting position.

Output should look like that

Input a list: 1 2 3 4
1,2,3,4
2,3,4,1
3,4,1,2
4,1,2,3
1,2,3,4

I have written a program that achieves the desired result but it dosent let the user input a number, it is just hardcoded into the program. How can I alter my code to produce the desired output?

see my code bellow:

def rotation(series): # change this to input a list
  for i in range(len(series) + 1):
    print(series)
    series = series[1:] + series[:1]

rotation([1,2,3,4])
mike.h67
  • 53
  • 4

1 Answers1

1

You can use the input function to get user input. The input would be a string so you would then have to split it using str.split method to create a list from the input.

like this:

user_input = input("Input a list: ")  # input = '1 2 3 4'
seq = user_input.split()   #  seq = ['1','2','3','4']

def rotation(nums):
    for i in range(len(nums)+1):
        rotated = nums[i:] + nums[:i]
        print(','.join(rotated))


rotation(seq)
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • 1
    awesome code works perfectly, is there a way to remove the output from showing numbers ['1','2','3','4'] and remove the single quotation marks from the numbers ? – mike.h67 May 10 '22 at 22:14
  • @mike.h67 is that what you are talking about? With the current edit the output should look identical to what you had in your question – Alexander May 10 '22 at 22:27
  • 1
    yep, that's perfect, thank you for helping me out – mike.h67 May 10 '22 at 22:38
  • @mike.h67 I actually edited it again... now it should look identical to your output. Glad I could help :) – Alexander May 10 '22 at 22:43