英文:
Why won't the user inputed value add to the created dictionary in the while loop in python?
问题
I tried creating a while loop that prompts the user to input the abbreviation for the state and if it is not already included in the dictionary to add it to the dictionary with the capital of the state. I have been having an issue where it won't add the abbreviation into the dictionary correctly and need some help trying to fix it. Thanks.
This is the code I used:
states = {'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark'}
length = len(states)
add_state = input('Enter state abrev. or Enter to quit ')
while add_state != '':
if add_state in states:
print(f'Already have {add_state}, capital', states[add_state])
add_state = input('Enter state abrev. or Enter to quit ')
else:
capital = input(f'Enter capital of {add_state} ')
add_state = input('Enter state abrev. or Enter to quit ')
states[add_state] = capital
print(states)
This is a sample output that I would get after:
Enter state abrev. or Enter to quit CA
Enter capital of CA sacremento
Enter state abrev. or Enter to quit
{'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark', 'CA': 'sacremento'}
CA was not included when inputting the values into the dictionary.
英文:
I tried creating a while loop that prompts the user to input the abbreviation for the state and if it is not already included in the dictionary to add it to the dictionary with the capital of the state. I have been having an issue where it won't add the abbreviation into the dictionary correctly and need some help trying to fix it. Thanks.
This is the code I used:
states = {'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark'}
length = len(states)
add_state = input('Enter state abrev. or Enter to quit ')
while add_state != '':
if add_state in states:
print(f'Already have {add_state}, capital', states[add_state])
add_state = input('Enter state abrev. or Enter to quit ')
else:
capital = input(f'Enter capital of {add_state} ')
add_state = input('Enter state abrev. or Enter to quit ')
states[add_state] = capital
print(states)
This is a sample output that I would get after:
Enter state abrev. or Enter to quit CA
Enter capital of CA sacremento
Enter state abrev. or Enter to quit
{'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark', '': 'sacremento'}
The CA was not included when inputting the values into the dictionary.
答案1
得分: 0
以下是您要翻译的内容:
原因是状态"CA"
没有包含在列表中,因为当您在else:
块中的行add_state = input('Enter state abrev. or Enter to quit ')
中重新分配add_state
变量时,它被分配为空字符串(""
),当您按回车键时。然后字符串"sacramento"
被保存在空字符串下。
尝试通过在while
循环中包含输入提示并删除其他提示(除了循环外的一个提示)来重新格式化您的代码:
states = {'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark'}
length = len(states)
add_state = input('Enter state abrev. or Enter to quit ')
while add_state != '':
if add_state in states:
print(f'Already have {add_state}, capital', states[add_state])
else:
capital = input(f'Enter capital of {add_state} ')
states[add_state] = capital
# Moved input prompt here:
add_state = input('Enter state abrev. or Enter to quit ')
print(states)
输出:
Enter state abrev. or Enter to quit CA
Enter capital of CA sacramento
Enter state abrev. or Enter to quit
{'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark', 'CA': 'sacramento'}
英文:
The reason the state "CA"
wasn't included in the list is because when you you reassigned the add_state
variable in the line add_state = input('Enter state abrev. or Enter to quit ')
in the else:
block. In this case it was assigned to an empty string (""
) when you hit enter. Then the string "sacramento"
was was saved under an empty string.
Try reformatting your code by including the input prompt inside the while
loop and removing the other prompts (except the one outside the while loop):
states = {'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark'}
length = len(states)
add_state = input('Enter state abrev. or Enter to quit ')
while add_state != '':
if add_state in states:
print(f'Already have {add_state}, capital', states[add_state])
else:
capital = input(f'Enter capital of {add_state} ')
states[add_state] = capital
# Moved input prompt here:
add_state = input('Enter state abrev. or Enter to quit ')
print(states)
Output:
Enter state abrev. or Enter to quit CA
Enter capital of CA sacramento
Enter state abrev. or Enter to quit
{'ID': 'Boise', 'VT': 'Montpelier', 'SD': 'Pierre', 'ND': 'Bismark', 'CA': 'sacramento'}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论