我想从用户那里获得一系列字符串,并将其放入列表中,然后打印出来
我也想在完成后关闭列表并打印
list = []
for i in list:
list[a]=input('the name of stings:')
list.append(list[a])
a +=
print(list)
答案 0 :(得分:1)
尝试一下:
list_ = []
not_done = True
while not_done:
inp = input('name of string : ')
if inp.lower() != 'done': # Put any string in stead of 'done' by which you intend to not take any more input
list_.append(inp)
else:
break
print(list_)
输出:
name of string : sd
name of string : se
name of string : gf
name of string : yh
name of string : done
['sd', 'se', 'gf', 'yh']
答案 1 :(得分:1)
您可以执行以下操作:
n = int(input())
my_list = list()
for i in range(n):
my_str = input('Enter string ')
my_list.append(my_str)
print('You entered', my_str)
print(my_list)
以下是示例(第一行采用数字,表示要输入多少次):
4
Enter string abc
You entered abc
['abc']
Enter string xyz
You entered xyz
['abc', 'xyz']
Enter string lmn
You entered lmn
['abc', 'xyz', 'lmn']
Enter string opq
You entered opq
['abc', 'xyz', 'lmn', 'opq']
答案 2 :(得分:0)
N = 10 # desired number of inputs
lst = [] # don't use `list` as it's resereved
for i in range(N):
lst.append(input('the name of strings: ')
print(lst)
答案 3 :(得分:0)
示例如下:
startService()
输出:
public class BackgroundTaskWorker extends Worker {
public BackgroundTaskWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@Override
public Result doWork() {
Log.i("WORKING","DOING SOME WORK");
Context con = getApplicationContext();
SharedPreferences preferences = con.getSharedPreferences(MainActivity.sharedPrefs, Context.MODE_PRIVATE);
SharedPreferences.Editor editPrefs = preferences.edit();
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
String s_day = preferences.getString("DAY","0");
int old_day = Integer.parseInt(s_day);
if(old_day == 0){
Log.i("WORKING","old_day default");
editPrefs.putString("DAY",Integer.toString(day));
editPrefs.commit();
return Result.success();
}
else if(day == old_day) {
Log.i("WORKING", "day=old_day default");
return Result.success();
}
else {
Log.i("WORKING","old_day change");
editPrefs.putString("DAY",Integer.toString(day));
editPrefs.commit();
Log.d("BISASAM","triggered");
DateFormat date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.GERMANY);
Date dat = new Date();
Log.d("TRIGGERDATE",date.format(dat));
editPrefs.putString("REC", "Receiver called "+date.format(dat));
NotificationCompat.Builder builder= new NotificationCompat.Builder(con,"ID");
builder.setContentTitle("ALARM FIRED");
builder.setContentText("WORKER");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setSmallIcon(R.drawable.kreuz);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
Log.d("BUILDCHECK","correct");
CharSequence name = "NotChannel";
String desc = "Test Channel for Planer";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("NOT",name,importance);
channel.setDescription(desc);
NotificationManager notManager = con.getSystemService(NotificationManager.class);
notManager.createNotificationChannel(channel);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(con);
builder.setChannelId("NOT");
notificationManager.notify(1,builder.build());
}
//TODO Test Tageswechsel Wiederholende Tasks
String today = preferences.getString("0",null);
String tomorrow = preferences.getString("1",null);
String next_week = preferences.getString("7",null);
String next_month = preferences.getString("30",null);
if(today != null) {
String[] repetitive = today.split(" ");
for (int j = 1; j < repetitive.length; j += 2) {
Log.d("PIKACHU",repetitive[j-1]);
switch(repetitive[j]){
case "1":
if(tomorrow!=null)
tomorrow += ","+ repetitive[j-1]+" "+repetitive[j];
else
tomorrow=repetitive[j-1]+" "+repetitive[j];
break;
case "7":
if(next_week!=null)
next_week += ","+ repetitive[j-1]+" "+repetitive[j];
else
next_week=repetitive[j-1]+" "+repetitive[j];
break;
case "30":
if(next_month!=null)
next_month += ","+ repetitive[j-1]+" "+repetitive[j];
else
next_month=repetitive[j-1]+" "+repetitive[j];
break;
default:
}
}
}
Log.d("PUTTING",tomorrow);
Log.d("PUTTING",next_week);
Log.d("PUTTING",next_month);
editPrefs.putString("1",tomorrow);
editPrefs.putString("7",next_week);
editPrefs.putString("30",next_month);
editPrefs.commit();
ArrayList<String> month = new ArrayList<>();
for (int i = 0; i < Jobs.month_length; i++) {
month.add(preferences.getString(Integer.toString(i),""));
}
for (int i=1;i<Jobs.month_length;i++){
month.set(i-1,month.get(i));
}
month.set(30,"");
for(int i=0;i<Jobs.month_length;i++){
editPrefs.putString(Integer.toString(i),month.get(i));
}
Log.d("COMMITED",month.toString());
editPrefs.commit();
}
// Indicate success or failure with your return value:
return Result.success();
}
}