下面是使用类和对象的简单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())
答案 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)