• Предмет: Информатика
  • Автор: vyshgorodskayaal
  • Вопрос задан 4 месяца назад

Помогите срочно пожалуйста !!!!!
Складіть опитувальник з вашого улюбленого предмета. У вікні опитувальника під запитанням розмiстiть кнопки «так» і «ні». Якщо користувач натисне правильну кнопку, то у відповідь має отримати вікно з повідомленням, яке пояснює відповідь на запитання. А якщо неправильну - тодi вiкно із цим поясненням буде мати вигляд вікна showerror.​


grus123: на пайтоні?
grus123: зараз на пишу на плюсах

Ответы

Ответ дал: grus123
0

Відповідь:

Пояснення:

Python

import tkinter as tk

from tkinter import messagebox

class MathQuiz:

   def __init__(self, master):

       self.master = master

       master.title("Math Quiz")

       # Create question and answer dictionary

       self.questions = {

           "What is 2 + 2?": "yes",

           "What is 10 - 5?": "yes",

           "What is 7 * 3?": "no",

           "What is 12 / 4?": "yes",

           "What is the square root of 64?": "yes"

       }

       # Create question label

       self.question_label = tk.Label(master, text="Answer Yes or No:")

       self.question_label.pack()

       # Loop through questions and create question buttons

       for question, answer in self.questions.items():

           question_button = tk.Button(

               master,

               text=question,

               command=lambda answer=answer: self.check_answer(answer)

           )

           question_button.pack()

   def check_answer(self, answer):

       if answer == "yes":

           messagebox.showinfo("Correct", "Correct!")

       else:

           messagebox.showerror("Incorrect", "Incorrect! The answer is Yes.")

root = tk.Tk()

quiz = MathQuiz(root)

root.mainloop()

C++

#include <QtWidgets/QApplication>

#include <QtWidgets/QWidget>

#include <QtWidgets/QVBoxLayout>

#include <QtWidgets/QLabel>

#include <QtWidgets/QPushButton>

#include <QtWidgets/QMessageBox>

class MathQuiz : public QWidget {

public:

   MathQuiz(QWidget* parent = nullptr) : QWidget(parent) {

       setWindowTitle("Math Quiz");

       // Create question and answer dictionary

       questions = {

           {"What is 2 + 2?", "yes"},

           {"What is 10 - 5?", "yes"},

           {"What is 7 * 3?", "no"},

           {"What is 12 / 4?", "yes"},

           {"What is the square root of 64?", "yes"}

       };

       // Create question layout

       QVBoxLayout* layout = new QVBoxLayout(this);

       // Loop through questions and create question buttons

       for (const auto& [question, answer] : questions) {

           QPushButton* button = new QPushButton(question, this);

           connect(button, &QPushButton::clicked, [this, answer]() {

               checkAnswer(answer);

           });

           layout->addWidget(button);

       }

       setLayout(layout);

   }

private:

   std::map<std::string, std::string> questions;

   void checkAnswer(const std::string& answer) {

       if (answer == "yes") {

           QMessageBox::information(this, "Correct", "Correct!");

       }

       else {

           QMessageBox::critical(this, "Incorrect", "Incorrect! The answer is Yes.");

       }

   }

};

int main(int argc, char* argv[]) {

   QApplication app(argc, argv);

   MathQuiz quiz;

   quiz.show();

   return app.exec();

}

Вас заинтересует