这是我的pyhton练习计划:
def shut_down(s):
return s
if yes():
shut_down("Shutting down")
elif no():
shut_down("Shutdown aborted")
else:
shut_down("Sorry")
给我的问题是:
答案 0 :(得分:1)
几点:
if yes():
这是错的。您想要将函数输入与yes进行比较。它应该是if s == 'yes':
。同样是为了休息。def shut_down(s):
,因此需要一个参数。您应该在将此函数称为shutdown(yes)
ret = shutdown(yes)
def shut_down(s):
if s == "yes":
r = "Shutting down"
elif s == "no":
r = "Shutdown aborted"
else:
r = "Sorry"
return r
ret = shut_down("yes")
print (ret)
答案 1 :(得分:0)
public class MyInventoryAppDemo {
public static void main(String[] args) {
new MyInventoryAppDemo();
}
public class MyInventoryDataSource {
JdbcDataSource ds = new JdbcDataSource();
Connection cnn;
public MyInventoryDataSource() {
PreparedStatement ps = null;
ds.setURL("jdbc:h2:mem:test");
try{
cnn = ds.getConnection();
//cnn.prepareCall("DROP TABLE ItemResource").execute();
cnn.prepareCall("CREATE TABLE ItemResource(id INT PRIMARY KEY, name VARCHAR(50)," +
"serial VARCHAR(50), location VARCHAR(50));").execute();
cnn.prepareCall("INSERT INTO ItemResource(id,name,serial,location) values(1,'Notebook','1235','warehouse')").execute();
cnn.prepareCall("INSERT INTO ItemResource(id,name,serial,location) values(2,'Desktop','12356','warehouse')").execute();
cnn.prepareCall("INSERT INTO ItemResource(id,name,serial,location) values(3,'Mainframe','12357','garage')").execute();
cnn.prepareCall("INSERT INTO ItemResource(id,name,serial,location) values(4,'Abacus','12358','warehouse')").execute();
}catch(Exception ex){
ex.printStackTrace();
}
}
private Connection getCnn() throws SQLException {
//return ds.getConnection();
return cnn;
}
public List<ItemResource> getItemResources(String byLocation, String bySerial) {
String sql = "SELECT * FROM ItemResource WHERE location like ? and serial like ?";
PreparedStatement ps = null;
ResultSet rs = null;
Connection cnn = null;
List<ItemResource> itemResourceList=null;
try {
cnn = getCnn();
ps = cnn.prepareCall(sql);
ps.setString(1, "%" + byLocation + "%");
ps.setString(2, "%" + bySerial + "%");
rs = ps.executeQuery();
itemResourceList = itemResourceMapper(rs);
} catch (Exception ex) {
ex.printStackTrace();
}finally {
try {
//rs.close();
//ps.close();
//cnn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
return itemResourceList;
}
private List<ItemResource> itemResourceMapper(ResultSet rs){
ItemResource item = null;
List<ItemResource> itemResourceList= new ArrayList<>();
try {
while (rs.next()) {
item = new ItemResource(
rs.getInt("id"),
rs.getString("name"),
rs.getString("serial"),
rs.getString("location")
);
itemResourceList.add(item);
}
}catch(Exception ex){
ex.printStackTrace();
}
return itemResourceList;
}
}
public MyInventoryAppDemo() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
MyInventoryDataSource ds = new MyInventoryDataSource();
ItemResourceModel model = new ItemResourceModel(ds.getItemResources("",""));
JTable table = new JTable(model);
JTextField serial = new JTextField(50);
JTextField location = new JTextField(50);
JButton jButton = new JButton("Filter");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ItemResourceModel model = new ItemResourceModel(ds.getItemResources(location.getText(),serial.getText()));
table.setModel(model);
model.fireTableDataChanged();
//table.repaint();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2,2));
frame.add(serial);
frame.add(location);
frame.add(jButton);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ItemResourceModel extends AbstractTableModel {
private List<ItemResource> itemResourceList;
ItemResourceMetaData itemResourceMetaData;
public ItemResourceModel(List<ItemResource> itemResourceList) {
this.itemResourceList = itemResourceList;
itemResourceMetaData = new ItemResourceMetaData();
}
@Override
public int getRowCount() {
return itemResourceList.size();
}
@Override
public int getColumnCount() {
return itemResourceMetaData.getColumns().length;
}
@Override
public String getColumnName(int column) {
return itemResourceMetaData.getColumns()[column];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return itemResourceMetaData.getColumnClass()[columnIndex];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ItemResource item = itemResourceList.get(rowIndex);
Object itemValue = null;
switch (columnIndex) {
case 0:
itemValue = item.getId();
break;
case 1:
itemValue = item.getName();
break;
case 2:
itemValue = item.getSerial();
break;
case 3:
itemValue = item.getLocation();
break;
}
return itemValue;
}
}
public class ItemResourceMetaData {
private String[] columns =
{"Id", "Name", "Serial", "Location"};
private Class[] columnClass =
{String.class, String.class, String.class, String.class};
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
public Class[] getColumnClass() {
return columnClass;
}
public void setColumnClass(Class[] columnClass) {
this.columnClass = columnClass;
}
}
public class ItemResource {
private int id;
private String name;
private String serial;
public ItemResource(int id, String name, String serial, String location) {
this.id = id;
this.name = name;
this.serial = serial;
this.location = location;
}
private String location;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
}
答案 2 :(得分:-1)
比最佳答案短的东西:
def shut_down(s):
if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"
print shut_down("algo")
答案 3 :(得分:-2)
正如其他人在评论中指出的那样,你的回归应该在你的功能的逻辑结束处。
def shut_down(s):
if s == "yes":
r = "Shutting down"
elif s == "no":
r = "Shutdown aborted"
else:
r = "Sorry"
return r