0

I'm writing a programme that will display messages that are stored in an array one at a time. I want it to be able to move through them one at a time upon command from the user. My problem is when it reaches the last value on the array I cannot figure out a way to get it to loop back to the first record.

I appreciate that this problem may seem obvious to some but I have struggled to come up with a solution. I would really appreciate if someone could show me a way to solve this.

1
  • 6
    Can you post your attempt at solution? Commented Feb 14, 2018 at 12:00

1 Answer 1

1

This must work.

import sys

array = [0,1,2,3,4,5,6]

x = 0
count = 0

while x == 0:
    user_input = int(input("Enter 1 to move in array or 2 to exit: "))

    if user_input == 1 and count < len(array):
        print(array[count])
        count = count + 1
    else:
        count = 0

    if user_input == 2:
        x = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Upd. Exit option to end while

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.