Take user input in Python 2.7

2019-03-03 13:05发布

Below is a simple python 2.7 code using class and objects.

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

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

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

How can this code be implemented so that the name and preference values(string) are taken using user-input(raw_input())

2条回答
smile是对你的礼貌
2楼-- · 2019-03-03 13:45

Here's an example:

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)
查看更多
迷人小祖宗
3楼-- · 2019-03-03 13:50

Here is also a working code.

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)
查看更多
登录 后发表回答