有没有一种方法可以实现用户输入以在我定义的此类中创建一个新条目?
class Pulsar:
'Collective base of all Pulsars'
pulsarCount = 0
def __init__(self, name, distance):
self.name = name
self.distance = distance
Pulsar.pulsarCount += 1
def displayCount(self):
print( "Total Pulsars %d" % Pulsar.pulsarCount)
def displayPulsar(self):
print( "Name : ", self.name, ", Distance: ", self.distance)
"This creates the first object"
pulsar1 = Pulsar("B1944+17", "979 Lightyears")
"This creates the second pulsar in the class"
pulsar2 = Pulsar("J2129-5721", "1305 Lightyears")
pulsar1.displayPulsar()
pulsar2.displayPulsar()
print( "Total Pulsars %d" % Pulsar.pulsarCount)
我希望用户能够输入自己的名称/距离并将其附加到当前定义的变量中。
答案 0 :(得分:1)
根据您对Pulsar对象的处理方式,一类可能会过分杀人。
function autoReply() {
var interval = 5; // if the script runs every 5 minutes; change otherwise
var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var noReply = ["ls@compass.com", "feedback@compass.com", "jenkins@compass.com", "shamirwehbe@me.com", "shamirwehbe@yahoo.com"];
var fromEmails = [];
var yesReply
var replyMessage = "Hello!\n\nYou have reached me during non business hours. I will respond by 9 AM next business day.\n\nIf you have any Compass.com related questions, check out Compass Academy! Learn about Compass' tools and get your questions answered at academy.compass.com.\n\nBest,\n\nShamir Wehbe";
if ([6,0].indexOf(day) > -1 || (hour < 9) || (hour >= 17)) {
var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
var threads = GmailApp.search('is:inbox after:' + timeFrom);
var label = GmailApp.getUserLabelByName("autoReplied");
for (var i = 0; i < threads.length; i++) {
var messagesFrom = threads[i].getMessages()[0].getFrom();
var email = messagesFrom.substring(messagesFrom.lastIndexOf("<") + 1, messagesFrom.lastIndexOf(">"));
fromEmails.push(email);
yesReply = fromEmails.filter(function(e) {
return noReply.indexOf(e) ==-1;});
for (var y = 0; y < fromEmails.length; y++) {
if (threads[i].isUnread() && yesReply.indexOf(fromEmails[y]) > -1){
threads[i].reply(replyMessage);
threads[i].markRead();
threads[i].addLabel(label);
}
}
}
}
}
答案 1 :(得分:0)
class Pulsar:
'Collective base of all Pulsars'
pulsarCount = 0
def __init__(self, name, distance):
self.name = name
self.distance = distance
Pulsar.pulsarCount += 1
def displayCount(self):
print( "Total Pulsars %d" % Pulsar.pulsarCount)
def displayPulsar(self):
print( "Name : ", self.name, ", Distance: ", self.distance)
"This creates the first object"
pulsar1 = Pulsar("B1944+17", "979 Lightyears")
"This creates the second pulsar in the class"
pulsar2 = Pulsar("J2129-5721", "1305 Lightyears")
pulsar1.displayPulsar()
pulsar2.displayPulsar()
print( "Total Pulsars %d" % Pulsar.pulsarCount)
# New code
users_name = input('Your name: ')
distance = input('The distance: ')
pulsar1.name = users_name
pulsar1.distance = distance
# Then you can wrap this in a function if you want