【发布时间】:2020-11-30 20:59:03
【问题描述】:
我是编程新手,也是 python 新手。我做了一些没有大问题的小项目(石头剪刀布和刽子手)。为了挑战自己,我试图在没有示例的情况下连续制作 4 场比赛。我创建了多个函数来代表游戏的一部分/步骤。
其中一个函数 (get_player_input) 负责处理用户输入。我要求用户选择一列。然后我检查多个东西(它是从 1 到 7 的 int 并且列不满吗?)。如果输入有效,我返回变量column_select 和free_places_column。我返回这些变量的原因是因为我想重用这些信息以使用第二个函数在游戏板上“放置一块”(place_piece)
这是我迷路的地方。我可以通过以下方式使用这些变量:column_select, free_places_column = get_player_input() 但是这段代码重新运行函数get_player_input。导致用户被询问两次他想在哪一栏中放一块。
到目前为止我的代码:
# The game 4 in a row
# Define the game table with 6 rows and 7 columns
game_board = [[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]]
# print the game board
def printboard():
print("|", game_board[0][0], "|", game_board[0][1], "|", game_board[0][2], "|", game_board[0][3], "|",
game_board[0][4], "|", game_board[0][5], "|", game_board[0][6], "|")
print("- - + - + - + - + - + - + - -")
print("|", game_board[1][0], "|", game_board[1][1], "|", game_board[1][2], "|", game_board[1][3], "|",
game_board[1][4], "|", game_board[1][5], "|", game_board[1][6], "|")
print("- - + - + - + - + - + - + - -")
print("|", game_board[2][0], "|", game_board[2][1], "|", game_board[2][2], "|", game_board[2][3], "|",
game_board[2][4], "|", game_board[2][5], "|", game_board[2][6], "|")
print("- - + - + - + - + - + - + - -")
print("|", game_board[3][0], "|", game_board[3][1], "|", game_board[3][2], "|", game_board[3][3], "|",
game_board[3][4], "|", game_board[3][5], "|", game_board[3][6], "|")
print("- - + - + - + - + - + - + - -")
print("|", game_board[4][0], "|", game_board[4][1], "|", game_board[4][2], "|", game_board[4][3], "|",
game_board[4][4], "|", game_board[4][5], "|", game_board[4][6], "|")
print("- - + - + - + - + - + - + - -")
print("|", game_board[5][0], "|", game_board[5][1], "|", game_board[5][2], "|", game_board[5][3], "|",
game_board[5][4], "|", game_board[5][5], "|", game_board[5][6], "|")
print("- - + - + - + - + - + - + - -")
print(" 1 2 3 4 5 6 7")
print()
def get_player_input():
# set varaibles
free_places_column = 0
received_valid_input = False
# Validate if player input is int and anywhere from 1 to 7. if not ask again
while received_valid_input == False:
try:
column_select = int(input("Which column (1-7) do you want to drop the piece: "))
if 0 < column_select < 8:
for i in range(0, 6):
if game_board[i][column_select - 1] == " ":
free_places_column = free_places_column + 1
if free_places_column == 0:
print("Column is full. please select an other column")
else:
received_valid_input = True
else:
print('Please provide a number between 1 and 7')
except ValueError:
print('Wrong input. Please enter a number between 1 and 7')
return column_select, free_places_column
def place_piece(player):
column_select, free_places_column = get_player_input()
print("Going to place a piece in column", column_select, "and row", free_places_column)
if player == "A":
game_board[free_places_column - 1][column_select - 1] = "X"
else:
game_board[free_places_column - 1][column_select - 1] = "O"
while True:
printboard()
get_player_input()
place_piece("A")
当我运行这段代码时,结果如下:
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
1 2 3 4 5 6 7
Which column (1-7) do you want to drop the piece: 1
Which column (1-7) do you want to drop the piece: 1
Going to place a piece in column 1 and row 6
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| | | | | | | |
- - + - + - + - + - + - + - -
| X | | | | | | |
- - + - + - + - + - + - + - -
1 2 3 4 5 6 7
我已经在互联网上搜索并观看了有关将变量从一个函数重新调整到另一个函数的 YouTube 教程,但我还没有找到如何处理这个问题。我发现了一些可以解决我的问题的方法,但目前似乎没有一个方法可以解决:
- 让我的变量全局化 --> 发现很多很多人告诉其他人不要这样做
- 开始使用类 --> 对我来说仍然是一个难以掌握的概念。也有点想在不完全放弃迄今为止的选择的情况下解决这个问题
- 抛弃函数,编写一大段代码 --> 努力学习使用函数
我还编写了一小段代码来消除一些复杂性。希望让我自己更容易理解:
def function1():
a = 10
b = 20
print("We are in function 1")
return a, b
def function2():
a, b = function1()
print(a, b)
print("We are in function 2")
function1()
function2()
结果:
We are in function 1
We are in function 1
10 20
We are in function 2
谁能指出我正确的方向?
最好的问候
【问题讨论】:
-
您能否更新最后的简单示例以显示您如何调用
function2()? -
我明白了。当 function2() 会为你调用时,不要自己调用 function1()。毕竟这是一种命令式编程语言。
-
代码审查评论:您可以使用 for 循环显着简化 printboard() 函数。对于新程序员来说将是一个很好的练习:)
-
我设法将它写成两个 for 循环。第二个 for 循环位于第一个 for 循环内。我试图在此处粘贴代码,但无法将其识别为代码。