tic-tac-toe - if语句没有在python的while循环中运行
问题描述
我在运行一些用于在井字游戏中输入的代码时遇到了问题。代码编写如下:
def player_input():
marker = " "
while marker != "X" and marker != "O":
marker = input("Player 1: Do you want me to play X or O? ").upper()
if marker == "X":
return ("X", "O")
else:
return ("O", "X")
玩家输入()
调用函数后,'if loop' 没有在文本编辑器中运行,而在 Jupyter notebook 中,代码返回元组,我不明白为什么!需要帮忙。我是初学者。
解决方案
You have missed the line indentations..And you didnt mention the method outside the code..
Line Indentations: In simple terms indentation refers to adding white space before a statement. But the question arises is it even necessary? To understand this consider a situation where you are reading a book and all of a sudden all the page numbers from the book went missing. So you don’t know, where to continue reading and you will get confused. This situation is similar with Python. Without indentation, Python does not know which statement to execute next or which statement belongs to which block.
Revised version of code....
def player_input():
marker = " "
while marker != "X" and marker != "O":
marker = input("Player do you want to play X or O").upper()
if marker == "X":
print("You have chosen X and if statement works...Insert your command here")
else:
print("You have chosen O and else statement works...Insert your command here")
player_input()
推荐阅读
- javascript - 如何在我的 .cshtml 页面中调用 Vue 组件?
- reactjs - 无法正确反应关闭织物面板
- python-3.x - 在标记化时我遇到了这个错误
- angularjs - Angular:检测到变量更改时如何自动重新渲染?
- python - 在 Python 中将命名空间放入不同的 XML 标记中
- php - 获得产品可见性价值 - WooCommerce
- flutter - mobx 可观察数据能否在多个屏幕之间持久存在?
- android - 数据绑定 - MediatorLiveData 未在 Fragment 内触发
- java - 将元素添加到模型时,JFace TreeViewer 不会更新
- php - 重定向错误,但仅 .htaccess 中的标准 WP-Rules