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

问题描述 投票:0回答:2

我正在尝试创建一个小游戏,其中玩家和他们的机器人有一组已经预定义的动作,它们应该做的伤害量,它是什么类型的攻击等等。当玩家/机器人攻击时,该程序应该总结该实体做了什么,将其打印到控制台,然后将其打印到文件。但是,摘要无法正确打印。

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()函数无关,我觉得它与将类似格式的字符串存储到变量然后调用变量就像我做的那样在代码中。我只是不知道为什么会这样,以及如何解决它,以便在我打印时,它打印时没有括号,引号和逗号。

python python-3.x
2个回答
2
投票

当你这样做:

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

你实际上正在创建一个元组,这是一系列单独的值,这就是为什么它以这种方式打印。

试试这个:

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

2
投票

代替

print(response)

尝试:

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

你的response对象是tuple - 所以python打印它像tuple。您希望格式化元组内的字符串,使其成为单个字符串。

编辑:你可能感到困惑,因为print('x', 'y')将打印x y - 只有当你分别将选项传递给print时才会发生。如果你将它们包装在一个元组并传递元组,那就没有了。为了达到同样的效果,你需要在调用print时解开元组。你可以试试:

print(*response)
© www.soinside.com 2019 - 2024. All rights reserved.