samedi 21 décembre 2019

Why does this randint not work in an if statement?

import time
import random
def printgrid():
  print(grid[0][0],grid[0][1],grid[0][2])
  print(grid[1][0],grid[1][1],grid[1][2])
  print(grid[2][0],grid[2][1],grid[2][2])
grid=[['-','-','-'],['-','-','-'],['-','-','-']]
print('TIP: It might be a good idea to turn on CAPSLOCK for this game.')
time.sleep(2)
printgrid()
print('This is the Noughts and Crosses board. Make your move: \n |topleft=TL   |topcenter=TC   |topright=TR  |\n |middleleft=ML|middlecenter=MC|middleright=MR|\n |bottomleft=BL|bottomcenter=BC|bottomright=BR|')
validmove=False
while validmove==False:
  move=str(input('move:'))
  if move=="TL":
    grid[0][0]='x'
    validmove=True
  elif move=="TC":
    grid[0][1]='x'
    validmove=True
  elif move=="TR":
    grid[0][2]='x'
    validmove=True
  elif move=="ML":
    grid[1][0]='x'
    validmove=True
  elif move=="MC":
    grid[1][1]='x'
    validmove=True
  elif move=="MR":
    grid[1][2]='x'
    validmove=True
  elif move=="BL":
    grid[2][0]='x'
    validmove=True
  elif move=="BC":
    grid[2][1]='x'
    validmove=True
  elif move=="BR":
    grid[2][2]='x'
    validmove=True
  else:
    print('invalid move')
printgrid()
omove=random.randint(0,8)
print(omove)
validmove=False
while validmove==False:
  omove=random.randint(0,8)
  print(omove)
  if omove=="0":
    grid[0][0]='o'
    validmove=True
  elif omove=="1":
    grid[0][1]='o'
    validmove=True
  elif omove=="2":
    grid[0][2]='o'
    validmove=True
  elif omove=="3":
    grid[1][0]='o'
    validmove=True
  elif omove=="4":
    grid[1][1]='o'
    validmove=True
  elif omove=="5":
    grid[1][2]='o'
    validmove=True
  elif omove=="6":
    grid[2][0]='o'
    validmove=True
  elif omove=="7":
    grid[2][1]='o'
    validmove=True
  elif omove=="8":
    grid[2][2]='o'
    validmove=True
  else:
    print('something is wrong')
    validmove=False
time.sleep(2)
printgrid()

It just continually out puts "something is wrong" and the omove it is using. BTW I am doing Naughts and crosses project. I have tried to make this work and checked everything so either i am blind(most likely) or there is somthing wrong with Repl.it (I am using Repl.it (https://repl.it/)).

Here is the output:

something is wrong
6
something is wrong
3
something is wrong
7
something is wrong
8
something is wrong
2
something is wrong
5
something is wrong
8
something is wrong
3



Aucun commentaire:

Enregistrer un commentaire