python输入input数组_Python学习——实现简单的交互raw_input的使用
row_input的使用:>>> name=raw_input("please input your name:")
please input your name:xiaobai
>>> name
'xiaobai'
編寫小程序,詢問用戶姓名,性別,年齡,工作,工資,以格式化的方式輸出:
Information of company stuff:
Name:
Age:
Sex:
Job:
代碼:[root@nfs-server ~]# vim information_of_stuff.py
#!/bin/python
name=raw_input("Please input your name:")
age=raw_input("Please input your age:")
sex=raw_input("Please input your sex:")
job=raw_input("Please input your job:")
print '''
==============================
Information of company stuff:
Name: %s
Age: %s
Sex: %s
Job: %s
=============================='''%(name,age,sex,job)
執行:[root@nfs-server ~]# python information_of_stuff.py
Please input your name:xiaobai
Please input your age:25
Please input your sex:male
Please input your job:engineer
==============================
Information of company stuff:
Name: xiaobai
Age: 25
Sex: male
Job: engineer
==============================
輸入一個0~100直接的數字讓用戶猜,并且猜測次數不能大于三次。[root@nfs-client2 ~]# vim guess_num.py
#!/bin/python
import os
os.system('clear') #執行時先清屏
real_num=int(raw_input("please input the real_num from 0 to 100:"))
os.system('clear') #輸入讓用戶猜的數字后清屏
retry_count=0 #設定循環關閉條件
while retry_count<3: #后面加冒號
guess_num=int(raw_input("Please input a number from 0 to 100:"))
if guess_num>real_num:
print "Wrong! Please try smaller!"
retry_count+=1 #自增
elif guess_num
print "Wrong! Please try bigger!"
retry_count+=1
else: #最后一個條件用
print "Congurations! You got it!"
break #跳出循環
else:
print "Too much times!"
Python不像shell,沒有fi循環關閉符號,而是通過縮進控制代碼層級,同一級代碼縮進應保持一致,if和else不屬于同一級,縮進不同也可執行,但不符合書寫規范。
raw_input輸入的是字符串,字符串與數字比較時會自動轉為ASCII值進行比較,因此要使用int將其轉換為整數類型,break為跳出循環。
ord:將字符串轉換為ASCII對應的值。>>> print ord("a")
97
>>> print ord("1")
49
優化代碼,以上代碼輸入回車或字符串會報錯,且數字不是隨機值,需要優化。[root@nfs-client2 ~]# vim guess_num.py
#!/bin/python
import os
import random
os.system('clear')
real_num=random.randrange(100)
os.system('clear')
retry_count=0
while retry_count<3:
guess_num=raw_input("Please input a number from 0 to 100:").strip() #去空格回車
if len(guess_num)==0: #判斷字符串長度是否為0
continue
if guess_num.isdigit(): #判斷是否全為數字
guess_num=int(guess_num)
else:
print "You should input a number instead of string!"
continue #跳出當前循環,進行下一次循環
if guess_num>real_num:
print "Wrong! Please try smaller!"
elif guess_num
print "Wrong! Please try bigger!"
else:
print "Congurations! You got the real number %d !"%real_num
break
retry_count+=1
else:
print "Too much times! The real number is",real_num
.strip()表示將輸入的空格和回車去掉;
len(guess_num)表示計算字符串的長度;
continue表示跳出當前循環,進行下一次循環;
isdigit()表示判斷是否全是數字;
將上述循環更改為for循環:[root@nfs-client2 ~]# vim guess_num_for.py
#!/bin/python
import os
import random
os.system('clear')
real_num=random.randrange(100)
os.system('clear')
for i in range(3):
guess_num=raw_input("Please input a number from 0 to 100:").strip()
if len(guess_num)==0:
continue
if guess_num.isdigit():
guess_num=int(guess_num)
else:
print "You should input a number instead of string!"
continue
if guess_num>real_num:
print "Wrong! Please try smaller!"
elif guess_num
print "Wrong! Please try bigger!"
else:
print "Congurations! You got the real number %d !"%real_num
break
else:
print "Too much times! The real number is",real_num
range為數組,其參數分別為起始值,末尾值,步長。>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(2,10,2)
[2, 4, 6, 8]
>>> range(2,20,5)
[2, 7, 12, 17]
設計一個多層循環,用戶輸入密碼正確的話進入目錄進行選擇,并能退出循環。[root@nfs-client2 ~]# cat mulit_loop.py
#!/bin/python
passwd="test"
logout=False #加跳出的flag
for i in range(3):
password=raw_input("Please input your password:").strip()
if len(password)==0:
continue
if password==passwd:
print "Welcome to login!"
while True:
user_select=raw_input('''
====================================
Please input a number to continue
1.Send files;
2.Send emalis;
3.exit this level;
4.exit the whole loop.
====================================
''').strip()
user_select=int(user_select)
if user_select==1:
print "Sending files as you wish!"
if user_select==2:
print "Sending emails as you wish!"
if user_select==3:
print "Exit this level,please re-input the password!"
break
if user_select==4:
print "Ok, let's have a break!"
logout=True
break
if logout==True:
break
總結
以上是生活随笔為你收集整理的python输入input数组_Python学习——实现简单的交互raw_input的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Word2010输入数学公式怎么插入矩阵
- 下一篇: windows电脑新硬盘怎么分区