Syed Imran Murtaza

Software Engineer

Front-end Developer

Freelancer

Programmer

0

No products in the cart.

Syed Imran Murtaza
Syed Imran Murtaza
Syed Imran Murtaza
Syed Imran Murtaza
Syed Imran Murtaza
Syed Imran Murtaza

Software Engineer

Front-end Developer

Freelancer

Programmer

Blog Post

Python Things

June 25, 2024 Blog

-3 Dimensional Array:

import numpy as np

arr = np.array([[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],

            [[10, 11, 12],
             [13, 14, 15],
             [16, 17, 18]],

            [[19, 20, 21],
             [22, 23, 24],
             [25, 26, 27]]])

print(“3D Array:”)
print(arr)
print()

print(“Accessing elements:”)
print(“Element at [0, 1, 2]:”, arr[0, 1, 2]) # Accessing element at slice 0, row 1, column 2
print(“Element at [1, 2, 0]:”, arr[1, 2, 0]) # Accessing element at slice 1, row 2, column 0
print(“Element at [2, 0, 1]:”, arr[2, 0, 1]) # Accessing element at slice 2, row 0, column 1

print(“Elements are: “, arr[2,2,2])

2D Array:

import numpy as np

arr = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
arr_np = np.array(arr)

Accessing elements in the NumPy array

print(arr_np[0, 3]) # Accessing element at row 0, column 3

Print the entire NumPy array

print(arr_np)

Exception Handling:

x=10

try:
if(x>10):
raise exception (“Sorry “)
except:
a=12
a=13
add = a+b
print(add)

Online Quiz System Project:

import time

Function to display welcome message and get user details

def welcome():
print(“Welcome to the Online Quiz!”)
name = input(“Enter your name: “)
class_name = input(“Enter your class name: “)
return name, class_name

Function to select quiz level

def select_quiz_level():
print(“\nSelect Quiz Level:”)
print(“1. Easy”)
print(“2. Medium”)
print(“3. Hard”)
while True:
choice = input(“Enter 1, 2, or 3 to select the quiz level: “)
if choice in [‘1’, ‘2’, ‘3’]:
return int(choice)
else:
print(“Invalid input. Please enter 1, 2, or 3.”)

Function to conduct quiz and return score

def conduct_quiz(level):
score = 0
results = [] # List to store results of each question

start_time = time.time()  # Start time of the quiz

if level == 1:
    questions = [
        {"question": "What is the capital of France?", "options": ["A. Paris", "B. London", "C. Rome", "D. Berlin"], "answer": "A"},
        {"question": "Which planet is known as the Red Planet?", "options": ["A. Jupiter", "B. Mars", "C. Venus", "D. Saturn"], "answer": "B"},
        {"question": "Who painted the Mona Lisa?", "options": ["A. Leonardo da Vinci", "B. Pablo Picasso", "C. Vincent van Gogh", "D. Michelangelo"], "answer": "A"}
    ]
elif level == 2:
    questions = [
        {"question": "What is the powerhouse of the cell?", "options": ["A. Nucleus", "B. Mitochondria", "C. Ribosome", "D. Golgi apparatus"], "answer": "B"},
        {"question": "Which country is the largest by land area?", "options": ["A. Russia", "B. Canada", "C. China", "D. United States"], "answer": "A"},
        {"question": "What year did World War I begin?", "options": ["A. 1914", "B. 1918", "C. 1939", "D. 1945"], "answer": "A"}
    ]
elif level == 3:
    questions = [
        {"question": "What is the chemical symbol for the element gold?", "options": ["A. Au", "B. Ag", "C. Fe", "D. Cu"], "answer": "A"},
        {"question": "Which of Shakespeare's plays is the longest?", "options": ["A. Hamlet", "B. Macbeth", "C. Romeo and Juliet", "D. Antony and Cleopatra"], "answer": "D"},
        {"question": "Who developed the theory of relativity?", "options": ["A. Isaac Newton", "B. Albert Einstein", "C. Galileo Galilei", "D. Nikola Tesla"], "answer": "B"}
    ]
else:
    print("Invalid quiz level selected.")
    return 0, []

for index, question in enumerate(questions, start=1):
    print(f"\nQuestion {index}: " + question["question"])
    for option in question["options"]:
        print(option)

    start_question_time = time.time()  # Start time for each question
    while True:
        user_answer = input("Enter your answer (A/B/C/D): ").upper()
        if user_answer in ['A', 'B', 'C', 'D']:
            break
        else:
            print("Invalid input. Please enter A, B, C, or D.")

    end_question_time = time.time()
    elapsed_question_time = end_question_time - start_question_time

    if elapsed_question_time > 20:
        print("Time's up! You took too long to answer this question.")
        results.append((question["question"], "Time's up"))
        continue

    if user_answer == question["answer"]:
        score += 1
        results.append((question["question"], "Correct"))
    else:
        results.append((question["question"], "Incorrect"))

end_time = time.time()  # End time of the quiz
total_time = end_time - start_time  # Total time taken for the quiz

return score, results, total_time

Function to calculate grade based on score

def calculate_grade(score):
if score == 3:
return “A”
elif score == 2:
return “B”
elif score == 1:
return “C”
else:
return “F”

Main program flow

def main():
name, class_name = welcome()
level = select_quiz_level()
score, results, total_time = conduct_quiz(level)
grade = calculate_grade(score)

print("\nQuiz Results:")
print("Name:", name)
print("Class:", class_name)
print("Score:", score)
print("Grade:", grade)
print("Total Time:", round(total_time, 2), "seconds")

print("\nQuestion Results:")
for index, (question, result) in enumerate(results, start=1):
    print(f"Question {index}: {question} - {result}")

if name == “main“:
main()

Write a comment