Ich lerne weiter Python und ich möchte den Wert einer Variablen als Schlüssel in einem Wörterbuch und die Eingabe des Benutzers als Wert dieses Schlüssels verwenden, aber was ich mache, ist das Gegenteil.Wie verwende ich den Wert der Variable als Schlüssel in einem Wörterbuch in Python?
def scan_option(option_type):
option_dict={}
elected_type = option_type + '_option'
option_dict.update(elected_type= raw_input("Enter the {} Option: ".format(option_type)))
return option_dict
print scan_option("Hardrive")
Falls Benutzer beantwortet 8: Die Ausgabe lautet: {'elected_type': '8'}
und ich will es sein: {'Hardrive_option': '8'}
Was muss ich in den Code ändern, das zu tun?
'option_dict [gewählter_typ] = raw_input (" Geben Sie die {} Option ein: ".format (option_type))' – Hamms
Awesome, ich wusste nicht, dass es so einfach war. Ich glaube, ich habe das Update des Wörterbuch falsch verwendet. –