python - 将输入的以空格分隔的项目添加到字典中。 Python

标签 python loops input dictionary spaces

我需要制作一个程序,要求用户输入城市,然后输入那里的温度。城市和温度被一个空间隔开。当用户输入“停止”时,程序将停止请求输入并报告最冷的城市。您始终可以假设用户输入一个城市,然后输入一个空格,然后输入温度。

Sample Output:

Enter city followed by temperature > Edmonton -2
Enter city followed by temperature > Toronto 3
Enter city followed by temperature > Vancouver -1
Enter city followed by temperature > Ottawa -3
Enter city followed by temperature > stop
{'Toronto': 3, 'Edmonton': -2, 'Vancouver': -1, 'Ottawa': -3}
The coldest city is Ottawa -3

我遇到的问题是如何将输入的项目放入字典表单中。我不明白它如何与输入项目中的空间一起使用。我还有其他三个程序遵循这种格式,输入包括空格,我真的无法弄清楚/找到如何解决它。任何帮助将不胜感激。

PS:我正在学习初学者的计算科学类(class)..是的..

最佳答案

您需要使用 split() 将输入拆分为键和值部分方法。

关于python - 将输入的以空格分隔的项目添加到字典中。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15754076/

相关文章:

python - 导入错误 : No module named site

loops - 在go模板中获取迭代器索引(consul-template)

python - 输入()错误 - NameError : name '...' is not defined

python - Windows 上的 GeoDjango : "Could not find the GDAL library"/ "OSError: [WinError 126] The specified module could not be found"

python - 使用 Python 中的 Pandas 模块将从网站提取的项目写入具有不同长度列表的 .xls 工作表

Python 搜索和替换是否重复替换字符串?

ruby - 为什么这个 ruby 每个循环都不会中断?

python - for循环中的条件

javascript - 这是受控还是不受控的 React 组件?

bash - 比较文件文件,提示不要求输入