跳至內容

面向非程式設計師的 Python 2.6 教程/誰在那裡?

來自華夏公益教科書,開放的書籍,面向開放的世界

輸入和變數

[編輯 | 編輯原始碼]

現在我覺得是時候寫一個真正複雜的程式了。它在這裡

print "Halt!"
user_reply = raw_input("Who goes there? ") 
print "You may pass,", user_reply

(使用者回應:使用 Linux 和 Geany 編輯器...唯一顯示的選項是 'user_return'...輸出正確。但是,當手動輸入 'user_reply' 時,也能正常工作,儘管 Geany 中沒有顯示為選項...這兩個之間有什麼功能上的區別?)

執行它時,我的螢幕顯示了以下內容

Halt!
Who goes there? Josh
You may pass, Josh

注意:在透過按下 F5 執行程式碼後,Python Shell 將只給出輸出

Halt!
Who goes there?

您需要在 Python Shell 中輸入您的姓名,然後按 Enter 鍵才能獲得其餘的輸出。

當然,當您執行程式時,由於raw_input()語句,您的螢幕將看起來不同。當您執行程式時,您可能已經注意到(您確實運行了程式,對吧?),您需要輸入您的姓名,然後按 Enter 鍵。然後程式打印出一些額外的文字,以及您的姓名。這是一個輸入的示例。程式到達某個點,然後等待使用者輸入一些資料,以便程式以後使用。

當然,如果我們沒有地方存放從使用者那裡獲得的資訊,那麼獲取使用者資訊將毫無用處,這就是變數的用武之地。在前面的程式中,user_reply是一個變數。變數就像一個可以儲存一些資料的盒子。以下是一個顯示變數示例的程式

a = 123.4
b23 = 'Spam'
first_name = "Bill"
b = 432
c = a + b
print "a + b is", c
print "first_name is", first_name
print "Sorted Parts, After Midnight or", b23

以下是輸出

a + b is 555.4
first_name is Bill
Sorted Parts, After Midnight or Spam

以上程式中的變數是ab23first_namebc。Python 中的變數可以儲存任何型別的資料 - 在本例中,我們儲存了一些字串(例如,“Bill”)和一些數字(例如,432)。

注意字串和變數名之間的區別。字串用引號標記,這告訴計算機“不要嘗試理解,只需按原樣獲取此文字”

print "first_name"

這將列印文字

first_name

按原樣。變數名沒有引號,指示計算機“使用我之前在此名稱下儲存的值”

print first_name

這將列印(在之前的示例之後)

Bill

好的,所以我們有這些叫做變數的盒子,以及可以放入變數中的資料。計算機將看到類似first_name = "Bill"這樣的行,並將其解釋為“將字串Bill放入盒子(或變數)first_name中”。之後,它會看到語句c = a + b,並將其解釋為“將a + b123.4 + 432的總和(等於555.4)放入c中”。語句的右側(a + b)被評估,結果儲存在左側的變數(c)中。這稱為賦值,您不應該將賦值等號(=)與這裡的數學意義上的“相等”混淆(這是==將在後面用到的)。

以下是另一個變數使用示例

a = 1
print a
a = a + 1
print a
a = a * 2
print a

當然,這是輸出

1
2
4

即使兩邊都是同一個變數,計算機仍然將其解釋為“首先找出要儲存的資料,然後找出資料要儲存的位置”。

在結束本章之前,再寫一個程式

number = input("Type in a number: ")
text = raw_input("Type in a string: ")
print "number =", number
print "number is a", type(number)
print "number * 2 =", number * 2
print "text =", text
print "text is a", type(text)
print "text * 2 =", text * 2

我得到的輸出是

Type in a Number: 12.34
Type in a String: Hello
number = 12.34
number is a <type 'float'>
number * 2 = 24.68
text = Hello
text is a <type 'str'>
text * 2 = HelloHello

注意number是使用input()獲取的,而text是使用raw_input()獲取的。raw_input()返回一個字串,而input()返回一個數字。當您希望使用者輸入一個數字時,請使用input(),但如果您希望使用者輸入一個字串,請使用raw_input()

程式的後半部分使用type(),它告訴您變數是什麼。數字的型別為intfloat,分別代表整數浮點數(主要用於小數)。文字字串的型別為str,代表字串。整數和浮點數可以使用數學函式進行運算,字串則不能。注意,當 Python 將一個數字乘以一個整數時,會發生預期的事情。但是,當字串乘以一個整數時,結果是字串的多個副本被生成(即,text * 2 = HelloHello)。

字串的運算與數字的運算不同。以下是一些互動模式示例,以進一步說明這一點。

>>> "This" + " " + "is" + " joined."
'This is joined.'
>>> "Ha, " * 5
'Ha, Ha, Ha, Ha, Ha, '
>>> "Ha, " * 5 + "ha!"
'Ha, Ha, Ha, Ha, Ha, ha!'
>>> 

這也可作為程式進行

print "This" + " " + "is" + " joined."
print "Ha, " * 5
print "Ha, " * 5 + "ha!"

以下是部分字串運算的列表

運算 符號 示例
重複 * "i" * 5 == "iiiii"
連線 + "Hello, " + "World!" == "Hello, World!"

Rate_times.py

# This program calculates rate and distance problems
print "Input a rate and a distance"
rate = input("Rate: ")
distance = input("Distance: ")
print "Time:", (distance / rate)

示例執行

Input a rate and a distance
Rate: 5
Distance: 10
Time: 2
Input a rate and a distance
Rate: 3.52
Distance: 45.6
Time: 12.9545454545

Area.py

# This program calculates the perimeter and area of a rectangle
print "Calculate information about a rectangle"
length = input("Length: ")
width = input("Width: ")
print "Area", length * width
print "Perimeter", 2 * length + 2 * width

示例執行

Calculate information about a rectangle
Length: 4
Width: 3
Area 12
Perimeter 14
Calculate information about a rectangle
Length: 2.53
Width: 5.2
Area 13.156
Perimeter 15.46

temperature.py

# Converts Fahrenheit to Celsius
temp = input("Fahrenheit temperature: ")
print (temp - 32.0) * 5.0 / 9.0

示例執行

Fahrenheit temperature: 32
0.0
Fahrenheit temperature: -40
-40.0
Fahrenheit temperature: 212
100.0
Fahrenheit temperature: 98.6
37.0
  1. 編寫一個程式,從使用者那裡獲取 2 個字串變數和 2 個整數變數,將它們連線起來(將它們組合在一起,中間沒有空格),並顯示這些字串,然後在新行中將這兩個數字相乘。
解決方案

編寫一個程式,從使用者那裡獲取 2 個字串變數和 2 個整數變數,將它們連線起來(將它們組合在一起,中間沒有空格),並顯示這些字串,然後在新行中將這兩個數字相乘。

  
string1 = raw_input('String 1: ')
string2 = raw_input('String 2: ')
int1 = input('Integer 1: ')
int2 = input('Integer 2: ')
print string1 + string2
print int1 * int2

另一個解決方案

print "this is an exercise"
number_1 = input("please input the first number: ")
number_2 = input("Please input the second number: ")

string_1 = raw_input("Please input the first half of the word: ")
string_2 = raw_input("please input the second half of the word: ")

print "the words you input is '" + string_1 + string_2 + "'"
print "the result of the 2 numbers is:", number_1 * number_2


華夏公益教科書