2016-06-09 138 views
0

我想寫一個文本冒險。起初,它詢問一個人是男性還是女性。如果他們輸入「男性」,我希望它使用他,他和他的話。如果他們輸入「女」,我想它使用的話她,她等我似乎無法分配東西

print ("Is that person male or female?") 
gender = input() 
if gender == "male": 
    1 = his 
    2 = he 
    3 = him 
else: 
    1 = her 
    2 = she 
    3 = her 

我想的話顯示,當我把在數字例如:

print (2,"went with", 3,) 

這應該說"he went with him""she went with her",但它不。

+1

Python中的'1','2'和'3'是無效的[標識符](https://docs.python.org/3/reference/lexical_analysis.html#identifiers) – jonrsharpe

+1

「這應該說些什麼,但它沒有。「這是對語法錯誤的輕描淡寫。無論如何,你還必須將'his'等放在引號中,因爲現在它希望找到一個名爲'his'的變量,而不是一個字符串。 –

回答

0

Python變量名的規則:

  1. 必須以字母開頭(A - Z,A - B)或下劃線(_)
  2. 其它字符可以是字母,數字或_
  3. 區分大小寫
  4. 可以是任何(合理的)長度
  5. 有一些保留字不能用作變量名稱,因爲Python將它們用於其他用途。

here

+1

請注意,這些是爲2.x而不是3.x,這是一個字母的定義有點自由(例如,你可以''從數學導入pi作爲π'!)。 – jonrsharpe

1
gender = input("Is that person male or female? ") 
if gender == "male": 
    one = "his" 
    two = "he" 
    three = "him" 
else: 
    one = "her" 
    two = "she" 
    three = "her" 
print(one+' item that '+two+' had was for '+three) 

這是怎麼可能看起來像一個例子。