我正在为学校项目编写的小型问答游戏需要帮助。 假设有3个问题,有多个选择。它是用OOP Python 3编写的。这是我第一次使用Python类。
代码如下:
class Questions:
def __init__(self, question, answer):
self.question = question
self.answer = answer
q_dict = [
"""Q1 Why is it important to scan your target network slowly?\n
A. To avoid alerting the IDS
B. It is not necessary to scan the network slowly."""'\n\n\n',
"""Q2 What is the difference between a traditional firewall and an IPS?
A. Firewalls do not generate logs.
D. IPS can dissect packets"""'\n\n\n',
"""Q3 What tool is able to conduct a man-in-the-Middle Attack on an 802.3 environment?
A. Ettercap
B. Cain & Abel"""'\n\n\n'
]
a_dict = [
Questions(q_dict[0], "A"),
Questions(q_dict[1], "D"),
Questions(q_dict[2], "B")
]
def start(a_dict):
points = 0
for question in a_dict:
answer = input(q_dict.question)
if answer == a_dict.answer:
points += 10
print("You got 10 points")
print("Total points: %s" % points)
start(a_dict)
当我尝试运行它时,我的程序抛出错误:
Traceback (most recent call last):
File "./test.py", line 36, in <module>
start(a_dict)
File "./test.py", line 30, in start
answer = input(a_dict.question)
AttributeError: 'list' object has no attribute 'question'
我希望它打印列表并提示用户输入他们的选择,如果答案与我设置为正确答案的答案相符,则给用户10分,然后显示总分。
答案 0 :(得分:1)
您正在遍历包含Question对象的列表。因此,您需要编写:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import static sun.misc.ClassFileTransformer.add;
public class DesktopEsimerkki extends JPanel
implements TableModelListener, ActionListener {
protected JTable table = null;
protected TableModel model = null;
public DesktopEsimerkki() {
super(new GridLayout(1, 0));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(this);
add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.model = table.getModel();
}
@Override
public void tableChanged(TableModelEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {
BufferedWriter bw = null;
String rivi = "";
FileWriter fw = null;
try {
fw = new FileWriter("data.csv");
bw = new BufferedWriter(fw);
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
if (j == 69) {
rivi += model.getValueAt(i, j);
} else {
rivi += model.getValueAt(i, j) + ",";
}
}
if (i != 69) {
rivi += "\n";
}
}
System.out.println(rivi);
bw.write(rivi);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public JMenuBar luoValikkoPalkki() {
JMenuBar valikkopalkki = new JMenuBar();
JMenu valikko = new JMenu("File");
valikko.setMnemonic(KeyEvent.VK_F);
valikko.getAccessibleContext().setAccessibleDescription(
"File Saving MEnu");
valikkopalkki.add(valikko);
JMenuItem valikkoitem = new JMenuItem("Save", KeyEvent.VK_S);
valikkoitem.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_S, ActionEvent.CTRL_MASK));
valikko.add(valikkoitem);
valikkoitem.addActionListener(this);
return valikkopalkki;
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Col 1", "Col 2", "Col 3"};
private Object[][] data = {{"Data 1", "Data 2", "Data 3"}};
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
return true;
}
@Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("DesktopEsimerkki");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DesktopEsimerkki desim = new DesktopEsimerkki();
frame.setJMenuBar(desim.luoValikkoPalkki());
DesktopEsimerkki newContentPane = new DesktopEsimerkki();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}