有没有办法给一个变量分配两个不同的值?

2024-09-30 18:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉,如果这个问题的措辞很糟糕,但我基本上想创建一个数组(不确定术语是否正确),第一列中有一个生物的名称,第二列中有一个对象的名称,然后第三列中有一个数字(放弃机会)。理想情况下,我可以使用生物名称引用此数组,以便输出对象名称并使用第三列的数字执行计算


# create dictionary of boss names with a numerical value attached

boss_name = {
    "1": "Vindicta and Gorvek",
    "2": "Gregorovic",
    "3": "Helwyr",
    "4": "Twin Furies"
}
print("") # space

# print the table of bosses (https://stackoverflow.com/questions/27067061/python-print-dictionary-using-column-formatting/43550837)
for item in boss_name:
    print(item, boss_name[item])

print("") # space

boss_input = input("Please enter the number corresponding to the boss you would like to calculcate the drop rates for: ")
#boss_input = re.sub(r'[a-z,.]','',boss_input.lower()) # converts input into numbers only - this line is redundant?
boss_input_integer = int(boss_input) # converts the input into an integer

# input error check
print(len(boss_name))
input_error = True # set default state

while input_error == True: # when an invalid input is made, this section will loop continuously until a valid one is entered

    if boss_input_integer in range (1,len(boss_name)+1):
        print(f"You have selected {boss_name.get(boss_input)}")
        input_error = False # The input is valid, therefore no input error was made
    else:
        print(f"Invalid input. Please choose a number between 1 and {len(boss_name)}") # Error message for when a number is entered that is greater than the number of entries in the boss list
        boss_input = input("Please enter the number corresponding to the boss you would like to calulcate the drop rates for: ") # allows the user to input a new value
        boss_input_integer = int(boss_input)
    


# Function 1: Boss pet chance calculator

pet_item_info = {
    "Vindicta and Gorvek" : "Imbued blade slice" : "1/2000",
    "Gregorovic" : "Faceless mask" "1/1000"
}

因此,当我输入示例2时,第一个字典将该值作为“Gregorovic”。然后它将检查数组(在函数1下)第一列中列出的Gregorovic的位置,然后将输出无脸掩码和1/1000,例如“您已选择Gregorovic;无脸掩码的下降率为1/1000”。我知道我不能在使用3列的情况下使用库,但我发现更容易想象我的问题

任何帮助都将不胜感激,谢谢


Tags: thetoname名称numberforinputis
2条回答

如果我理解正确,你想把“第三栏”改成新行。因此,根据这一点,如果你想进行更多的黑客攻击,那么你可以使用:

pet_item_info = {
    "Vindicta and Gorvek" : "Imbued blade slice \n 1/2000",
    "Gregorovic" : "Faceless mask \n 1/1000"
}

字符串运算符\n是一个换行符,类似于HTML中的<br>

不过,你绝对应该看看W3学校的python tutorial

否则,您可以以各种方式拥有多个列。正如@OneCricketeer所说,我也建议编写一个Boss类

如果我理解这个问题,您希望在单个条目下存储一些数据集合

用“专栏”来思考是非常有限的。在我看来,最好从文档/字段的角度来考虑

boss_name = {
  "2": {
     "name": "Gregorovic", 
     "item": {"name": "Faceless mask", "rate": "1/1000"}
  }
}

ans = input("Which boss: ")
boss = boss_name[ans]["name"]
item = boss_name[ans]["item"]

print("You have selected {}; {} has a drop rate of {}".format(boss, item['name'], item['rate']))

相关问题 更多 >