1

I want to simplify the if statements instead of typing fifth_turn == each_turn_before

table() fifth_turn = int(input('Player #1, select the spot you desire: ')) if fifth_turn == first_turn or fifth_turn == second_turn or fifth_turn == third_turn or fifth_turn == fourth_turn: print('Spot Taken') elif fifth_turn == 1: spot1 = 'x' elif fifth_turn == 2: spot2 = 'x' elif fifth_turn == 3: spot3 = 'x' elif fifth_turn == 4: spot4 = 'x' elif fifth_turn == 5: spot5 = 'x' elif fifth_turn == 6: spot6 = 'x' elif fifth_turn == 7: spot7 = 'x' elif fifth_turn == 8: spot8 = 'x' elif fifth_turn == 9: spot9 = 'x' else: print('ERROR') 
3
  • Please format the code properly. Commented Jan 22, 2019 at 3:48
  • 1
    And come up with a decent title please. Commented Jan 22, 2019 at 3:53
  • 2
    This can be simplified by keeping the turns in a list and running through the list with a for-loop Commented Jan 22, 2019 at 3:55

2 Answers 2

1

There is a lot that can be improved in your code by organizing spots into a list and using the in operator:

spots = [spot1, spot2, spot3, ... spot9] # Initialize the list fifth_turn = int(input('Player #1, select the spot you desire: ')) if fifth_turn in first_turn, second_turn, third_turn, fourth_turn: print('Spot Taken') elif 1 <= fifth_turn <= 9: spots[fifth_turn - 1] = 'x' else: print('ERROR') 

Incidentally, printing "ERROR" is very uninformative because it does not tell the user what actually happened and how to fix it.

You should also consider having a list of turns instead of five (or more?) turn variables:

spots = [spot1, spot2, spot3, ... spot9] # Initialize the list turns = [...] # Initialize the list turns[4] = int(input('Player #1, select the spot you desire: ')) if turns[4] in turns[:4]: print('Spot Taken') elif 1 <= turns[4] <= 9: spots[turns[4] - 1] = 'x' else: print('ERROR') 

If you are trying to program tic-tac-toe, other optimizations are possible (like not having the list of turns at all).

Sign up to request clarification or add additional context in comments.

Comments

0

If I do not misunderstand what you're trying to do, I think it can be done more efficiently using a list.

spot = ['O' for i in range(9)] # ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] turn = int(input('Player #1, select the spot you desire: ')) if spot[turn - 1] == 'X': # Zero-indexed list print("Spot taken") else: spot[turn - 1] = 'X' # e.g. ['O', 'O', 'O', 'X', 'O', 'O', 'O', 'O', 'O'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.