Python教程

Python学习:input()输入的是字符串

本文主要是介绍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()输入的是字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!