为什么用户输入的值不会在Python的while循环中添加到创建的字典中?

huangapple go评论70阅读模式
英文:

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'}

huangapple
  • 本文由 发表于 2023年5月7日 06:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定