Python学习:input()输入的是字符串
2021/9/16 22:04:46
本文主要是介绍Python学习:input()输入的是字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目的:通过flag来实现跳出while循环
编译结果:无法跳出while循环
原因:input()输入的是字符串而不是False;同理,即便输入0,"等等,都被编译器认为是字符串而不是表示False
flag=True while flag: print(flag) singer_name_input=input("which singer do you like?\n") album_name_input=input("what album of him do you like\n") flag=input("if you want to break,"+ "just input False\n") print(flag) def make_album(singer_name,album_name,songs_number=''): """build a dictionary about albums""" if songs_number: collection={ 'singer_name':singer_name.title(), 'album_name':album_name.title(), } collection['songs_number']=songs_number else: collection={ 'singer_name':singer_name.title(), 'album_name':album_name.title(), } return collection album=make_album(singer_name_input,album_name_input) print(album)
解决方案:如下
flag=True while flag: singer_name_input=input("which singer do you like?\n") album_name_input=input("what album of him do you like\n") answer=input("if you want to break,"+ "just input False\n") if answer=="False": flag=False
这篇关于Python学习:input()输入的是字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程