简体   繁体   English

在Python 2.7中进行用户输入

[英]Take user input in Python 2.7

Below is a simple python 2.7 code using class and objects . 以下是使用对象的简单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)

How can this code be implemented so that the name and preference values( string ) are taken using user-input( raw_input() ) 如何实现此代码,以便使用用户输入( raw_input() )获取名称首选项值( 字符串

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM