2016-06-09 98 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) 

这是怎么可能看起来像一个例子。