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

时间:2019-04-20 17:03:50

标签: python python-3.x

我正在尝试创建一个小型游戏,其中玩家和他们的机器人具有一组预定义的动作,这些动作应设定为他们应该造成的伤害,其攻击类型等。 / bots攻击,该程序应该总结该实体的工作,将其打印到控制台,然后将其打印到文件中。但是,摘要不能正确打印。

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

输出:

('- - 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

我正在使用python 3.6.2,所以我知道它与print()函数无关,并且我觉得它与将这样格式化的字符串存储到变量中有关,然后像我在代码中那样调用变量。我只是真的不知道为什么会这样,以及如何解决这个问题,以便在我打印时打印时不会出现括号,引号和逗号。

2 个答案:

答案 0 :(得分:2)

代替

print(response)

尝试:

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

您的response对象是tuple-因此python像tuple一样打印它。您想格式化元组中的字符串,使其成为单个字符串。

编辑:您可能会感到困惑,因为print('x', 'y')将打印x y-仅在将选项分别传递给print时才会发生。如果将它们包装在一个元组中并通过该元组,则不会。为了获得相同的效果,您需要在调用print期间解开元组。您可以尝试:

print(*response)

答案 1 :(得分:2)

执行此操作时:

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

您实际上是在制作一个元组,该元组是一系列单独的值,因此它会以这种方式打印。

尝试以下方法:

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