Python:将输入声明为变量(特别是字典)

时间:2016-03-04 20:36:45

标签: python string variables dictionary input

一般来说是Python和编程的新手,也许我提出错误的问题,但我似乎无法找到我正在寻找的东西。 所以,我试图接受一个输入:

user = input('What is your character's name?")

并将其定义为字典,然后在添加统计数据时添加到字典中,程序会对派生统计数据进行数学运算并添加到它们中,这类东西。

因此,如果输入是' John Doe',我将空白除去:

usr = ''.join(user.split())

然后我如何获取结果字符串' JohnDoe',并将其定义为字典类型?我将其保留在程序中以使其更加舒适,然后我最终将寻找创建文件,它将使用GUI和所有内容为不同用户加载/保存。但那是现在很长的一段时间。我这样做并使用字典的键打印格式化的表格。

2 个答案:

答案 0 :(得分:0)

character_dict = {'Name': 'None', 'Class': 'None'}

character_dict['Name'] = str(raw_input('Enter a Character Name: '))
character_dict['Class'] = str(raw_input('Choose a Class: '))

print ("The hero " + character_dict['Name'] + " is a level 0 " + character_dict['Class'])

答案 1 :(得分:0)

也许这个例子会让你对dictonary如何运作有所了解:

# Create an empty dictornary. 
data = dict()
# Lets put some examples in it this time. 
# Lets say, UserName:Password
data = {'Sara':'BrownCat2','John':'HelloKitty77','Magnus':'AngryCar101'}

# Show what we have in the dictonay.
print(data) 

# Inserting/Updating
data['John']='Yello1'
# Deleting 
del data['Magnus']

# Show what we have in the dictonay.
print(data) 

# Empty the whole dictonary 
data.clear()

# Show what we have in the dictonay.
print(data)