input和while循環——Python編程從入門到實踐


input( )

input()函數:讓程序運行暫停,等待用戶輸入。

message = input('Tell me something, and I will repeat it back to you: ')
print(message)

運行結果:

Tell me something, and I will repeat it back to you: Hello Python!
Hello Python!

 1. 編寫清晰的程序

name = input("Please enter your name: ")
print("Hello, " + name + "!")

Please enter your name: hery
Hello, hery!

提示信息超過一行時:

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += '\nWhat is your name? '
name = input(prompt)
print("\nHello, " + name + "!")

2. 獲取數值的輸入

age = input("How old are you? ")
print(type(age))

How old are you? 12
<class 'str'>

通過input()函數輸入的信息以字符串的形式存儲,若需要將輸入作為數值使用怎么辦呢?

可以使用int()函數將其轉換為數值表示:

height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
    print("\nYou're tall enough to ride!")
else:
    print("\nYou'll be able to ride when you're a little older.")

3. 求模運算符

求模運算符(%):求得兩數相除返回的余數。

可用於判斷一個數是奇數還是偶數:

number = input("Enter a number, and I'll tell you if it is even or odd: ")
number = int(number)
if number % 2 == 0:
    print('\nThe number ' + str(number) + ' is even.')
else:
    print('\nThe number ' + str(number) + ' is odd.')

運算符兩端的元素類型要一致,故print語句中又需要將數值型通過str()函數轉換為字符型。

while循環

for循環是針對集合中的每個元素的一個代碼塊,而while循環是不斷的運行,直到指定條件不滿足。

1. 使用while循環

current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

運行結果:

1
2
3
4
5

2. 讓用戶選擇何時退出

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ''
while message != 'quit':
    message = input(prompt)
    print(message)

運行結果:

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. Hello Python
Hello Python

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. Hello 0629
Hello 0629

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. quit
quit

 輸入為 quit 時循環結束。

若不想將 quit 也作為一條消息打印出來,則:

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ''
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

3. 使用標志

在要求很多條件都滿足的情況下才繼續運行的程序中,可定義一個變量,用於判斷整個程序是否處於活動狀態,這個變量稱為標志。

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

active = True
while active:
    message = input(prompt)

    if message == 'quit':
        active = False
    else:
        print(message)

敲代碼的時候把 active = False 敲成了 active = 'False',然后輸入quit還一直執行循環,哈哈哈

4. 使用break退出循環

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "

while True:
    city = input(prompt)

    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")

 Note: Python循環(while循環、for循環)中都可使用break語句來推出循環。

5. 在循環中使用continue

循環中使用continue,會返回大循環開頭,並根據條件測試結果決定是否繼續執行循環:

current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    else:
        print(current_number)

運行結果:

1
3
5
7
9

6. 避免無限循環

x = 1
while x < 5:
    print(x)
    x += 1

上述的代碼塊中,若漏寫了代碼行 x += 1,這個程序將沒完沒了地運行。可按Ctrl + C,也可關閉顯示程序輸出的終端窗口,或關閉編輯器,結束無限循環。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM