Python 實(shí)現(xiàn)一行輸入多個數(shù)字(用空格隔開)
很多人都會使用以下代碼,來實(shí)現(xiàn)多個字符串的連續(xù)輸入,但是這是以換行進(jìn)行操作的,而有些題目需要將一些數(shù)字進(jìn)行一行輸入
a=input()b=input()
1.實(shí)現(xiàn)一行輸入多個數(shù)字,并以空格隔開。
a,b=map(int,input().split())print(a,b)print(type(a)) #運(yùn)行結(jié)果1 21 2<class ’int’>
2.實(shí)現(xiàn)一個輸入多個單詞,每個單詞之間用逗號隔開。
str1,str2=map(str,input().split(’,’))print(str1,str2)print(type(str1)) #運(yùn)行結(jié)果love,chinalove china<class ’str’>
例題:將二進(jìn)制日期翻譯為十進(jìn)制的形式并輸出。例如,日期 00010 00000 00010 00000 00001 00011,翻譯為2020年1月3日。int(x,2):將一個二進(jìn)制的數(shù)字準(zhǔn)換為十進(jìn)制。
print('請輸入報(bào)道日期,每位二進(jìn)制數(shù)之間用空格隔開')sstr=''year1,year2,year3,year4,month,day=map(str,input().split())sstr=sstr+str(int(year1,2))+str(int(year2,2))+str(int(year3,2))+str(int(year4,2))+'年'sstr=sstr+str(int(month,2))+'月'sstr=sstr+str(int(day,2))+'日'print(sstr) #運(yùn)行結(jié)果請輸入報(bào)道日期,每位二進(jìn)制數(shù)之間用空格隔開00010 00000 00001 00100 00100 000012014年4月1日
到此這篇關(guān)于Python 實(shí)現(xiàn)一行輸入多個數(shù)字(用空格隔開)的文章就介紹到這了,更多相關(guān)Python一行輸入多個數(shù)字內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. js實(shí)現(xiàn)跳一跳小游戲2. 10個提供免費(fèi)PHP腳本下載的網(wǎng)站3. SpringBoot 開發(fā)提速神器 Lombok+MybatisPlus+SwaggerUI4. php5.6不能擴(kuò)展redis.so的解決方法5. js實(shí)現(xiàn)貪吃蛇小游戲(加墻)6. 使用idea 去除 html 代碼前的行號和空行的方法詳解7. JVM之class文件結(jié)構(gòu)8. Python編寫nmap掃描工具9. PHP設(shè)計(jì)模式(四)原型模式Prototype實(shí)例詳解【創(chuàng)建型】10. python 爬取嗶哩嗶哩up主信息和投稿視頻
