python第7章实训作业_试图学Python赚外快的第7天
感覺吧,越來越多了,今天主要要學while循環的,本來打算一天一章,一本書20章的話很快就搞定了,我覺得還是有點天真了 前邊還好,后邊打的程序查錯?查半天,主要是漢子切換時忘了換回來,標點符號錯的,還有縮進行 感覺把前面注意的通通都犯了一遍,有時候查好幾次,查不出毛病,你就說氣不氣,程序員沒頭發我突然理解了 今天就開始學習吧,硬是留了點小尾巴課后題,圖片就放在后邊不在中間放了哦
第7章:While循環和用戶輸入
1、Input()讓程序能夠暫停,使得用戶可以輸入信息
如下例子:
message=input("Tell me something,and I will repeat it back to you: ")
print(message)
2、如果提示語太長可以使用+=使得信息分行轉化,輸入行只能在+=這一行
name=input("please enter your name: ")
print("Hello, "+name +"!")
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt +="nWhat is your first name? "
name=input(prompt)
print("nHello, "+name+"!")
3、int()輸入數值,不要和str()記混了哦,這個是我們第二章說過的,將非字符串顯示為字符串
height=int(input("How tall are you,in inches?"))
#也可以在這一行寫height=int(hight)
if height >=36:
print("nYou're tall enough to ride!")
else:
print("nYou will be able to ride when you are a little older.")
4、余數運算用%,比如5%4余數為1(求模運算)
下面是輸入一個數顯示為奇數或者偶數
number=int(input("Enter a number, and I'll tell you if it's even or odd:"))
if number%2==0:
print("nThe number "+str(number)+" is even")
else:
print("nThe number "+str(number)+" is odd")
5、使用while循環語句(嗯,while循環就是當條件不滿足時停止,for循環是針對每個元素都要執行遍的)還有布爾表達,開啟程序運行時可以使用哦,當然滿足后要終止程序
#程序1
prompt="nTell me something ,and I'll repeat it back to you:"
prompt+="nEnter quit to end the program:"
message=""
while message!='quit':
message=input(prompt)
print(message) #會有一個quit打印出來
#程序1的改進,程序2
prompt="nTell me something ,and I'll repeat it back to you:"
prompt+="nEnter quit to end the program:"
message=""
while message!='quit':
message=input(prompt)
if message!='quit': #只有當message不等于quit才打印,當meesage等于quit時,就不會出現末尾的quit
print(message)
#程序2的改進,含標志(布爾表達式)
prompt="nTell me something ,and I'll repeat it back to you:"
prompt+="nEnter quit to end the program:"
active='True'
while active:
message=input(prompt)
if message=='quit':
active=False #當message不等于quit時,程序運行不下去,返回接著用戶重新輸入,直到message等于quit時運行打印
else:
print(message)
好了,就到這吧,溜了溜了
總結
以上是生活随笔為你收集整理的python第7章实训作业_试图学Python赚外快的第7天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音商城双 11 阶段性战报:定金预售商
- 下一篇: 小米公布双11终极战报 224亿创历年大