从Python字典调用对象后去除多余符号

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

当我从字典中选择一个对象时,它会显示

Element(Name='Hydrogen', Symbol='H', Atomic_Number=1, Atomic_weight=1.008, State='Gas (Reactive nonmetal)', Melting_point=-259.1, Boiling_point=-252.76, Oxidation_states='±1', Electron_Configuration='1s¹', Atomic_radius=37, Electronegativity=2.18, Valance_electrons=1)
,然后在其下方显示
Element: None
。 我们能把引号、开头和结尾的元素等多余的符号都去掉吗?我要注意的另一件事是,是否可以摆脱
def show():
或者我是否需要它,具体取决于我的代码的设置方式。

这是我的代码。

elements
是字典名称。

from dataclasses import dataclass
def gs(x):
    normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
    super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
    res1 = x.maketrans(''.join(normal),''.join(super_s))
    return x.translate(res1)
@dataclass
class Element():
    Name: str
    Symbol: str
    Atomic_Number: int #atomic number
    Atomic_weight: float
    State: str #metal, metalloid, gas or unknown at 0C and Classification
    Melting_point: float #melting point
    Boiling_point: float #boiling point
    Oxidation_states: str #oxidation States (charges)
    Electron_Configuration: str #electron Configuration
    Atomic_radius: int
    Electronegativity: float
    Valance_electrons: int
    def show(Name):
        print(Name)

elements = {
    'hydrogen':  Element('Hydrogen', 'H', 1, 1.008, 'Gas (Reactive nonmetal)',-259.1, -252.76, '±1', '1s{}'.format(gs('1')), 37, 2.18, 1),
}

while True:
    selection = input("Select the element by name: ")
    if selection in elements:
        print(f"Element: {elements[selection].show()}")
    elif 'h' in selection:
        hc = input("Please select the number that corresponds to your problem: \n1. Don't know how to spell the element \n2. Info is incorrect \n3. Cancel \n")
        if '1' in hc:
            print("Filler")
        elif '2' in hc:
            print("Please go to this google forms to report problem \n(Link)")
        elif '3' in hc:
            continue
    else:
        print("Element not found \nType if you need help type 'h'")
python dictionary
1个回答
0
投票

发布修复比“评论”更容易

from dataclasses import dataclass
def gs(x):
    normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
    super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
    res1 = x.maketrans(''.join(normal),''.join(super_s))
    return x.translate(res1)
@dataclass
class Element():
    Name: str
    Symbol: str
    Atomic_Number: int #atomic number
    Atomic_weight: float
    State: str #metal, metalloid, gas or unknown at 0C and Classification
    Melting_point: float #melting point
    Boiling_point: float #boiling point
    Oxidation_states: str #oxidation States (charges)
    Electron_Configuration: str #electron Configuration
    Atomic_radius: int
    Electronegativity: float
    Valance_electrons: int
    def show(Name):
        print(f"Element: {Name}")

elements = {
    'hydrogen':  Element('Hydrogen', 'H', 1, 1.008, 'Gas (Reactive nonmetal)',-259.1, -252.76, '±1', '1s{}'.format(gs('1')), 37, 2.18, 1),
}

while True:
    selection = input("Select the element by name: (h:for help, q:to quit) :").lower()
    if selection in elements:
        elements[selection].show()
    elif 'q' in selection:
        print("so long and thanks'f for all the fish")
        break
    elif 'h' in selection:
        hc = input("Please select the number that corresponds to your problem: \n1. Don't know how to spell the element \n2. Info is incorrect \n3. Cancel \n")
        if '1' in hc:
            print("Filler")
        elif '2' in hc:
            print("Please go to this google forms to report problem \n(Link)")
        elif '3' in hc:
            continue
    else:
        print("Element not found \nType if you need help type 'h'")
© www.soinside.com 2019 - 2024. All rights reserved.