为什么这不是用python打印的?
我试图打印一些简单的ascII艺术,但没有一个显示出来,我做错了什么?我认为这是可行的,因为这个人首先必须输入一些东西才能继续。 我只想做一个简单的石头和剪刀游戏。如果这与python 3.9.4有关的话,我也会使用python 3.9.4为什么这不是用python打印的?,python,python-3.x,Python,Python 3.x,我试图打印一些简单的ascII艺术,但没有一个显示出来,我做错了什么?我认为这是可行的,因为这个人首先必须输入一些东西才能继续。 我只想做一个简单的石头和剪刀游戏。如果这与python 3.9.4有关的话,我也会使用python 3.9.4 import random import time import ctypes import os def Main_Game(): y = input("Enter choice: ") b = random.cho
import random
import time
import ctypes
import os
def Main_Game():
y = input("Enter choice: ")
b = random.choice(choices)
# both put same; draw
if y == b:
print("Game ended with a draw!")
print("Player chose = " + y + "| Bot chose = " + b)
# player puts rock and bot puts paper; bot wins
elif y == "rock" and b == "paper":
print("Bot won the match with paper!")
print("Player chose = " + y + "| Bot chose = " + b)
# player puts paper and bot puts rock; player wins
elif y == "paper" and b == "rock":
print("Player won the match with paper!")
print("Player chose = " + y + " | Bot chose = " + b)
# player puts paper and bot puts scissors; bot wins
elif y == "paper" and b == "scissors":
print("Bot won the match with scissors!")
print("Player chose = " + y + " | Bot chose = " + b)
# player puts scissors and bot puts paper; player wins
elif y == "scissors" and b == "paper":
print("Player won the match with scissors!")
print("Player chose = " + y + " | Bot chose = " + b)
# player puts rock and bot puts scissors; player wins
elif y == "rock" and b == "scissors":
print("Player won the match with rock!")
print("Player chose = " + y + " | Bot chose = " + b)
# player puts scissors and bot puts rock; bot wins
elif y == "scissors" and b == "rock":
print("Bot won the match with rock!")
print("Player chose = " + y + " | Bot chose = " + b)
elif y == 'rock':
print("""
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
""")
print("""
# # #####
# # # #
# # #
# # #####
# # #
# # # #
# #####
""")
elif y == 'paper':
print("""
_______
---' ____)____
______)
_______)
_______)
---.__________)
""")
print("""
# # #####
# # # #
# # #
# # #####
# # #
# # # #
# #####
""")
elif y == 'scissors':
print("""
_______
---' ____)____
______)
__________)
(____)
---.__(___)
""")
print("""
# # #####
# # # #
# # #
# # #####
# # #
# # # #
# #####
""")
time.sleep(3)
clear()
Main_Game()
clear = lambda: os.system("cls")
choices = ["rock", "paper", "scissors"]
ctypes.windll.kernel32.SetConsoleTitleW("Playing rock, paper, scissors!")
Main_Game()
elif
仅在以前的分支都为false时检查其条件,并且您的分支处理所有可能的输入。如果y=='rock',您可能想让elif y=='rock'
行成为,因此无论前面条件的值如何,我们都会检查条件。您应该尝试绘制游戏流程图。
在您的代码中,当玩家输入一个选项时,您将从bot生成一个随机选项,并使用if和elif对其进行比较,但是当一条语句匹配时,我们假设
elif y=='rock'和b=='scissors':
则流将停止,因为只有在前面的语句不匹配时,代码才会通过elif语句
因此,您应该通过中断此部分来更改代码
elif y==“剪刀”和b==“石头”:
打印(“机器人赢得了与岩石的比赛!”)
打印(“玩家选择=“+y+”|机器人选择=“+b”)
如果y==‘岩石’:
因此,它也执行ASCII打印语句西尔维奥的回答解决了您眼前的问题,但我想我会发布一些关于如何稍微清理此代码的建议
首先,ascii art数据使逻辑有点难以读取,我将在脚本开始时将其移动到一些常量值:
ROCK_ART = """
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
"""
PAPER_ART = """
_______
---' ____)____
______)
_______)
_______)
---.__________)
"""
SCISSORS_ART = """
_______
---' ____)____
______)
__________)
(____)
---.__(___)
"""
VS_ART = """
# # #####
# # # #
# # #
# # #####
# # #
# # # #
# #####
"""
您可以定义字典,以便轻松查找这些图像
ART = {
"rock": ROCK_ART,
"paper": PAPER_ART,
"scissors": SCISSORS_ART,
"vs": VS_ART,
}
现在有很多重复的逻辑在检查谁赢。想象一下,如果你想编程玩。要检查的选项很多,硬代码的值也很多。如果我们把这些规则放到字典里呢
MATCH_UPS = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper",
}
要想知道谁赢了一场比赛,你可以做if MATCH\u UPS[y]==b
,看看用户是否赢了。注意,对变量使用更具描述性的名称,否则会很快让您感到困惑
总而言之:
def main_game():
user_choice = input("Enter choice: ")
bot_choice = random.choice(list(MATCH_UPS.keys()))
print(ART[user_choice])
print(ART["vs"])
print(ART[bot_choice])
print(f"Player chose = {user_choice} | Bot chose = {bot_choice}")
if user_choice == bot_choice:
print("Game ended with a draw!")
elif MATCH_UPS[user_choice] == bot_choice:
print(f"Player won the match with {user_choice}!")
else:
print(f"Bot won the match with {bot_choice}!")
time.sleep(3)
os.system("cls")
main_game()
if __name__ == "__main__":
main_game()
事实并非如此
elif y=='rock':
换成
if y=='rock':
因为您要打印播放器输入的ascii码,并且它不取决于系统的选择。可能是因为y
末尾附加了\n
,因此它与您的任何选择都不匹配。试着打印(repr(y))
看看到底有什么。伙计,谢谢你,我一定会在以后的项目中这样做的。