简体   繁体   English

当打印带有字典/列表的字符串时,如何防止括号和引号被打印?

[英]How to prevent parentheses and quotation marks from being printed when printing a string with dictionaries/lists?

I am trying to create a small game, where a player and their bots have a set of actions that have been predefined with the amount of damage they are supposed to do, what type of attack it is, etc. When the player/bots attack, the program is supposed to summarise what that entity did, print that to the console, and then print it to a file. 我正在尝试创建一个小型游戏,其中玩家和他们的机器人具有一组预定义的动作,这些动作应包含他们应该造成的伤害,其攻击类型等。当玩家/机器人攻击时,该程序应该总结该实体的工作,将其打印到控制台,然后将其打印到文件。 However, the summary does not print right. 但是,摘要不能正确打印。

import os, sys
import yaml
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from pygame.locals import *
import random
import time
#Defines the actions that each bot can take in the game. Goes as...Action[Damage, Damage type]
#Defines what kind of bot is present. PL - Player/ Player bots. EN - Enemy bots
## Start of Player 1 Information
comp1_Attacks = {'ATK': [20, 'NORMAL'], 'ICE':[30,'ICE'],'FIRE': [40,'FIRE']}
comp1_Stats = {'WEAKNESS': ['ICE',100], 'HP': 400000, 'TYPE':"PL"}
#Stores the values for
one_val = list(comp1_Attacks.values())
one_key = list(comp1_Attacks.keys())
one_stat_val = list(comp1_Stats.values())
one_stat_key = list(comp1_Stats.keys())
###
##Start of Bot 2 Information (Bot 1 is player)
comp2_Attacks = {'ATK': [20, 'NORMAL'], 'ICE':[30,'ICE'],'FIRE': [40,'FIRE'],'TYPE':"PL"}
comp2_Stats = {'WEAKNESS': ['ICE',100], 'HP': 400000, 'TYPE':"PL"}
two_val = list(comp2_Attacks.values())
two_key = list(comp2_Attacks.keys())
two_stat_val = list(comp2_Stats.values())
two_stat_key = list(comp2_Stats.keys())
###
##Start of Bot 3 Information
comp3_Attacks = {'ATK': [20, 'NORMAL'], 'ICE':[30,'ICE'],'FIRE': [40,'FIRE'],'TYPE':"PL"}
comp3_Stats = {'WEAKNESS': ['ICE',100], 'HP': 400000, 'TYPE':"PL"}
three_val = list(comp3_Attacks.values())
three_key = list(comp3_Attacks.keys())
three_stat_val = list(comp3_Stats.values())
three_stat_key = list(comp3_Stats.keys())
##
##Start of Bot 4 Information
comp4_Attacks = {'ATK': [20, 'NORMAL'], 'ICE':[30,'ICE'],'FIRE': [40,'FIRE'], 'TYPE':"PL"}
comp4_Stats = {'WEAKNESS': ['ICE',100], 'HP': 400000, 'TYPE':"PL"}
four_val = list(comp4_Attacks.values())
four_key = list(comp4_Attacks.keys())
four_stat_val = list(comp4_Stats.values())
four_stat_key = list(comp4_Stats.keys())
##
##Start of Enemy Information
ENEM_Attacks = {'ATK': [20, 'NORMAL'], 'ICE':[30,'ICE'],'FIRE': [40,'FIRE'], 'TYPE':"PL"}
ENEM_Stat = {'WEAKNESS': ['ICE',100], 'HP': 400000, 'TYPE':"EN"}
EN_val = list(ENEM_Attacks.values())
EN_key = list(ENEM_Attacks.keys())
EN_stat_val = list(ENEM_Stat.values())
EN_stat_key = list(ENEM_Stat.keys())
##
done = False
turn = 1
overall = ""
def DMG_Effect(a):
    dmg = ENEM_Stat['HP'] - a
    ENEM_Stat['HP'] = dmg
    print("Enemy now has",ENEM_Stat['HP'], "HP")
def fileopen(a,b):
    opener = open(a,'a')
    with open(a,'a') as yaml_file:
        yaml.dump(str(b), yaml_file,default_flow_style = True)
    opener.close()
user = input()
while not done:
    if turn == 5:
        turn = 1
    if turn == 1:
        print("What attack do you want to do?")
        for key, value in comp1_Attacks.items():
            print(key)
        user = input()
        if comp1_Attacks[user][1] == ENEM_Stat['WEAKNESS'][0]:
            overall =   ENEM_Stat["WEAKNESS"][1] + comp1_Attacks[user][0]
            response = '- - PLAYER used ',user, 'on the enemy. It did ',overall,' damage'
        else:
            overall = comp1_Attacks[user][0]
            response = '- - PLAYER used ', user, 'on the enemy. It did', overall,' damage'
        print (response)
        fileopen("BOT1.yml",response)
        DMG_Effect(overall)
    if turn == 2:
        dec = random.randint(0, 2)
        if two_val[dec][1]== ENEM_Stat['WEAKNESS'][0]:
            overall = ENEM_Stat["WEAKNESS"][1] + two_val[dec][0]
            response = '- - COMP2 used ', str(two_key[dec]), ' on the enemy. It did', overall, ' damage'
        else:
            overall = two_val[dec][0]
            response = '- - COMP2 used ', str (two_key[dec]), ' on the enemy. It did', overall , ' damage'
        print(response)
        fileopen("BOT2.yml", response)
        DMG_Effect(overall)
        time.sleep(2)
    if turn == 3:
        dec = random.randint(0, 2)
        if three_val[dec][1]== ENEM_Stat['WEAKNESS'][0]:
            overall = ENEM_Stat["WEAKNESS"][1] + three_val[dec][0]
            response = "- - COMP3 used ", three_key[dec], " on the enemy. It did", overall, " damage"
        else:
            overall = three_val[dec][0]
            response = "- - COMP3 used ",three_key[dec], " on the enemy. It did", overall, " damage"
        print(response)
        fileopen("BOT3.yml", response)
        DMG_Effect(overall)
        time.sleep(2)
    if turn == 4:
        dec = random.randint(0, 2)
        if four_val[dec][1]== ENEM_Stat['WEAKNESS'][0]:
            overall = ENEM_Stat["WEAKNESS"][1] + four_val[dec][0]
            response = "- - COMP4 used ", four_key[dec], " on the enemy. It did", overall, " damage"
        else:
            overall = four_val[dec][0]
            response = "- - COMP4 used ",four_key[dec], " on the enemy. It did", overall, " damage"
        print(response)
        fileopen("BOT4.yml", response)
        DMG_Effect(overall)
        time.sleep(2)
    turn = turn +1

Output: 输出:

('- - PLAYER used ', 'FIRE', 'on the enemy. It did', 40, ' damage')
Enemy now has 399960 HP
('- - COMP2 used ', 'FIRE', ' on the enemy. It did', 40, ' damage')
Enemy now has 399920 HP
('- - COMP3 used ', 'ATK', ' on the enemy. It did', 20, ' damage')
Enemy now has 399900 HP
('- - COMP4 used ', 'FIRE', ' on the enemy. It did', 40, ' damage')
Enemy now has 399860 HP
What attack do you want to do?
ATK
ICE
FIRE

I am using python 3.6.2, so I know it has nothing to do with the print() function, and I feel like it has something to do with storing a string that is formatted like that into a variable then calling the variable like how I did in the code. 我正在使用python 3.6.2,所以我知道它与print()函数无关,并且我感觉它与将这样格式化的字符串存储到变量然后像如何调用变量一样我在代码中做了。 I just don't really know why this is the case, and how to get around it so that, when I print, it prints without the parentheses, quotation marks, and commas. 我只是真的不知道为什么会这样,以及如何解决这个问题,以便在我打印时打印出的内容没有括号,引号和逗号。

Instead of 代替

print(response)

Try: 尝试:

print(" ".join(str(x) for x in response))

Your response object is a tuple - so python is printing it like a tuple . 您的response对象是一个tuple -因此python像tuple一样打印它。 You want to format the strings inside the tuple so that it's a single string. 您想格式化元组中的字符串,使其成为单个字符串。

EDIT: You probably got confused because print('x', 'y') will print xy - that only happens if you pass the options to print separately. 编辑:您可能会感到困惑,因为print('x', 'y')将打印xy仅当您传递单独print的选项时才会发生。 Not if you wrap them in a tuple and pass the tuple. 如果将它们包装在一个元组中并通过该元组,则不会。 To achieve the same effect you need to unravel the tuple during the call to print . 为了获得相同的效果,您需要在调用print解开元组。 You can try: 你可以试试:

print(*response)

When you do this: 执行此操作时:

response = '- - PLAYER used ',user, 'on the enemy. It did ',overall,' damage'

You're actually making a tuple, which is a sequence of separate values, which is why it prints that way. 您实际上是在制作一个元组,该元组是一系列独立的值,因此它以这种方式打印。

Try this instead: 尝试以下方法:

response = '- - PLAYER used ' + user + ' on the enemy. It did ' + str(overall) + ' damage'

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

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