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

pk脉冲下降

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

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()函数无关,并且我感觉它与将这样格式化的字符串存储到变量中然后像我一样在调用该变量中进行操作有关编码。我只是真的不知道为什么会这样,以及如何解决这个问题,以便在我打印时打印出的内容没有括号,引号和逗号。

约翰·戈登

执行此操作时:

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

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

尝试以下方法:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从列表中打印元素时,如何防止打印括号和引号?

如何打印带有引号的 Fortran 字符串?

打印列表时如何去掉字符串引号?

PHP打印带有多个单引号的字符串

使用带有 std::cout 的单引号打印字符串实际打印数字

JavaScript打印数组转换为带括号和引号的字符串

打印带有字符串的整数时出错

如何打印没有花括号和单引号的排序字典(python)

如何打印字典字符串

在没有括号和引号的循环中打印列表元素

如何在 Python 中打印带有字符串的列表?

Python:如何打印不带引号和方括号的列表

使用Hibernate时如何打印带有参数值的查询字符串

如何在不带引号的情况下打印字典的字符串值?

如何从字典中打印带有可变数量的键+值的格式化字符串

有没有一种方法可以防止部分字符串被打印出来?

此字符串将被打印多少次?

打印字符串时删除单引号?

使用 .format 时如何在字符串中打印大括号字符?

如何打印带有索引号的列表(python)?

如何避免在双引号字符串中打印变量

如何在括号内打印字符串列表?

通过列表推导以带有 f 字符串的格式化形式打印元组列表

如何通过在单引号内添加变量数据和字符串来打印

如何使用awk命令打印没有双引号的空字符串

PDF如何防止其自身被打印?

在字符串内打印单引号

Python 2.7:打印不带括号和引号的字典

在括号之间打印或返回字符串