Take user input in Python 2.7

人走茶凉 提交于 2019-12-04 07:23:10

问题


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())


回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/42185570/take-user-input-in-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!