在Python 2.7中获取用户输入

时间:2017-02-12 08:23:31

标签: python python-2.7 class user-input

下面是使用对象的简单python 2.7代码。

class Student:
 def __init__(self,name,pref):
  self.name = name
  self.preference = pref

student1=Student("Tom","cce")

print(student1.name)
print(student1.preference)

如何实施此代码,以便使用用户输入获取名称偏好值(字符串)(的raw_input()

2 个答案:

答案 0 :(得分:1)

这也是一个有效的代码。

class Student:
 def __init__(self,name,pref):
  self.name = name
  self.preference = pref

student1=Student(raw_input("enter name:"),raw_input("enter branch:"))

print(student1.name)
print(student1.preference)

答案 1 :(得分:0)

以下是一个例子:

class Student:
 def __init__(self,name,pref):
  self.name = name
  self.preference = pref

name1 = raw_input("Name:")
pref1 = raw_input("Preference:")

student1 = Student(name1, pref1)

print(student1.name)
print(student1.preference)