简体   繁体   English

Python代码适用于2.7,但不适用于3.5

[英]Python code works in 2.7 but not in 3.5

My coursework is to create Tic Tac Toe in Python, my tutor helped me get it working in 2.7 however It needs to be in 3.5. 我的课程是用Python创建Tic Tac Toe,我的导师帮助我在2.7中使它工作,但是它必须在3.5中。

Firstly in 2.7 the code below prints a 3x3 list, however in 3.5 it just prints the list downwards not 3x3. 首先,在2.7中,下面的代码打印3x3列表,但是在3.5中,它只是向下打印列表,而不是3x3。 my tutor said maybe put end = ' ' at the end but that also doesn't work. 我的导师说,也许在end = ' '处放end = ' ' ,但这也不起作用。

def printBoard( board ):
    counter = 0   
    for y in range(3):    
        for x in range(3):    
            print (board[counter]),    
            counter += 1    
        print    
    print

second problem is on 2.7 it allows me to continue to input numbers till the board is filled with X or O, on 3.5 it only allows to input once and then the program ends? 第二个问题是在2.7上,它允许我继续输入数字,直到板子上填满X或O;在3.5上,它只允许输入一次,然后程序结束?

value = input("input number between 1 and 9")    
value = int(value)        
if value == 1:    
    alist[0] = player1    
    printBoard( alist )    
    value = input("input number between 1 and 9")    
if value == 2:    
    alist[1] = player1    
    printBoard( alist )    
    value = input("input number between 1 and 9")

etc. 等等

  1. print changed from statement to a function in Python 3.x. 在Python 3.x中, print语句更改为函数 To print a statement without newline, you need to pass end=' ' parameter (You can use the print as a function in Python 2.7 if you put from __future__ import print_function at the beginning of the code): 要打印不带换行符的语句,您需要传递end=' '参数(如果您from __future__ import print_function放在代码开头,则可以将print作为Python 2.7中的函数使用):

     print(board[counter], end=' ') 
  2. input returns a string in Python 3.x. input在Python 3.x中返回一个字符串。 (does not evaluate the input string). (不评估输入字符串)。 You need to convert the value into int every where you used input : 您需要在使用input每个位置将值转换为int

     value = input("input number between 1 and 9") value = int(value) 

    Alternatively, instead of comparing the input with integer literal 1 or 2 , compare the input string with strings: '1' , '2' without converting the string into integer. 另外,也可以将输入字符串与字符串'1''2'进行比较,而不是将输入字符串与整数立即数12进行比较,而无需将字符串转换为整数。 (But this requires you to use raw_input in Python 2.7 instead of input ) (但这要求您在Python 2.7中使用raw_input而不是input

  3. print should be called: print() . print应该被称为: print() Otherwise, nothing is printed. 否则,不打印任何内容。

I assume board is something like [['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']] . 我假设board[['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']] That means you have an easy way of printing this with a single print() call. 这意味着您可以通过一个简单的print()调用来轻松打印此内容。

print(*(''.join(row) for row in board), sep='\n')

This joins each row into a new string, producing each row as part of a generator. 这会将每一行连接到一个新的字符串中,从而将每一行作为生成器的一部分生成。 This generator is unpacked with * and sent to print() , where each row is separated by a newline. 该生成器用*拆包,并发送到print() ,其中每行用换行符分隔。

For your second issue, the problem is simple: you cast int() for the first value , but not for the subsequent ones. 对于第二个问题,问题很简单:您将int()为第一个value ,但不转换为后续的值。 However, this is the sort of thing you should be doing with a loop. 但是,这是您应该使用循环执行的操作。 It'll prevent exactly this kind of bug. 它将完全防止此类错误。 If you find yourself writing lots of code with Ctrl + V , you're doing something wrong. 如果发现自己使用Ctrl + V编写了大量代码,则说明您做错了什么。 If each block is slightly different, with an incremented number, you would do that with something like for i in range(n): , which allows you to execute the same code with an incremented number on each iteration. 如果每个块都有一个递增的数字,那么您可以使用for i in range(n):来执行此操作,这使您可以在每次迭代中以递增的数字执行相同的代码。

However, I'd recommend a simple while loop that checks if the game is complete: 但是,我建议您使用一个简单的while循环来检查游戏是否完成:

while True:
    move = request_move()
    do_move('X', move)
    if game_complete():
        break
    request_move()
    do_move('O', move)
    if game_complete():
        break

You would then write appropriate functions to request move coordinates, input moves into the board, and check if the game is complete yet. 然后,您将编写适当的函数来请求移动坐标,将输入输入到棋盘中,并检查游戏是否完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM