so I got this code:
from random import randint
mylist = [1, 2, 3, 4 , 5]
enemy = 2
# it's going to be randint(1, 5)
print(enemy)
user = int(input("Enter a number between 1 and 5: ")) # user input 1
user = n-1
if enemy == mylist[user+1] or enemy == mylist[user+3]:
print("You win!")
elif enemy == mylist[user]:
print ("It's a draw")
else:
print ("You lose")
Okay, yay user won since the algorithm for wining is: you win (your input n) against numbers n+1 and n+3. So if user input is 1 (mylist[0]) he will win against 2 (mylist[user+1]) and 4 (mylist[user+3]). But the issue is when user will input number 5 I will get.
IndexError: list index out of range
Is there a way to pass it?
user = int(input("Enter a number between 1 and 5: ")) # user input 5
user = n-1 # 4
if enemy == mylist[user+1] or enemy == mylist[user+3]:
"""what you can do so that the mylist[4+1] won't equal index mylist[5] which
is out of list range but instead it will go back to mylist[0]?"""
print("You win!")
elif enemy == mylist[user]:
print ("It's a draw")
else:
print ("You loose")
Thank you in advance, I'm really stuck.