Python每日一練(13)-IQ智商判斷及測試

1. IQ智商判斷

智商,即智力商數(Intelligence Quotient),是衡量個人智力高低的標準。智商主要反映人的認知能力、思維能力、語言能力、觀察能力、計算能力、律動能力等。也就是說,它主要表現人的理性的能力。智商可以通過一系列的標準測試測量人在其年齡段的智力發展水平,如表1所示,爲9級智商評測表格。請編寫一個程序,根據提供的9級智商評測表格和用戶輸入的智商分數,輸出對應的智商等級和類別,運行效果如圖1、圖2所示。
在這裏插入圖片描述
在這裏插入圖片描述
示例代碼如下:

iq_score = int(input("請輸入你的IQ分數: ").strip())  # 錄入數據
if iq_score >= 140:  # 判斷
    print(f"您的IQ分數爲: {iq_score},智商9級: 天才")
elif 120 <= iq_score < 140:
    print(f"您的IQ分數爲: {iq_score},智商8級: 精英")
elif 110 <= iq_score < 120:
    print(f"您的IQ分數爲: {iq_score},智商7級: 人才")
elif 90 <= iq_score < 110:
    print(f"您的IQ分數爲: {iq_score},智商6級: 聰慧")
elif 80 <= iq_score < 90:
    print(f"您的IQ分數爲: {iq_score},智商5級: 凡人")
elif 70 <= iq_score < 80:
    print(f"您的IQ分數爲: {iq_score},智商4級: 臨界")
elif 60 <= iq_score < 70:
    print(f"您的IQ分數爲: {iq_score},智商3級: 智弱")
elif 50 <= iq_score < 60:
    print(f"您的IQ分數爲: {iq_score},智商2級: 智障")
else:
    print(f"您的IQ分數爲: {iq_score},智商1級: 白癡")

2. 2行代碼進行IQ智商判斷

只用兩行代碼,根據用戶輸入的IQ智商分數與表1的9級智商評測表進行匹配,並輸出IQ智商類別。運行效果如圖1、圖2所示。
在這裏插入圖片描述
在這裏插入圖片描述
示例代碼如下:

msg = ["白癡", "白癡", "白癡", "白癡", "白癡", "智障", "智弱", "臨界", "凡人", "聰慧", "人才", "精英",
       "天才", "天才", "天才", "天才", "天才", "天才", "天才"]
print(msg[int(int(input("請輸入你的IQ分數(在0-190範圍輸入): ").strip()) / 10)])

3. IQ智商測試

請編寫一個程序,根據本例提供的30道智商測試題來測試智商,每題回答正確加6分,回答錯誤不記分,完成本次測試,程序輸出智商分數。運行效果如下圖1、圖2所示。因爲30道題目控制檯輸出太長,所以就截取了開頭和結尾,省略了中間的答題。
在這裏插入圖片描述
這篇博文所用到的的文件資源網盤路徑如下:

鏈接:https://pan.baidu.com/s/1e6oWo-O61epKnp8wuQ4Ugg 
提取碼:wrod 

示例代碼如下:

import os


def read_file(path, file_name):
    """
    讀取文件函數
    :param path: 路徑
    :param file_name: 文件名稱
    :return: 文件內容
    """
    # 這裏的/ 也可以換成\\
    with open(path + "/" + file_name, "r", encoding="utf8") as file:
        content = file.read()  # 因爲文件內容比較少 所以直接使用read方法一次性全部讀取
    return content


if __name__ == '__main__':
    print("=======================IQ智力測試=======================")
    print("-" * 55)
    len_que = len(os.listdir("./que"))
    score = 0  # 用來統計得分
    # 30道題 每次循環讀取一道題 但是如果增加到了40道題
    # 是不是for循環又要改了 所以最好是動態獲取文件夾中的文件個數
    for i in range(1, len_que + 1):
        # 讀取文件 因爲讀取文件會在後續頻繁使用到 所以封裝爲函數
        print(f"\n第 {i} 題: \n" + read_file("./que", "que" + str(i) + ".txt"))
        # 用戶輸入答案
        user_ans = input("請輸入正確答案前面的數字編號: ").strip()
        # 讀取正確答案
        right_ans = read_file("./ans", "ans" + str(i) + ".txt").strip()
        if user_ans == right_ans:  # 判斷用戶輸入答案與正確一致
            score += 6  # 答案一致加6分
	print(f"你的IQ測試成績爲: {score}")

4. IQ智商測試與判斷(順序版)

完善上面的IQ智商測試程序,根據IQ智商測試分數和表1的9級智商評測表輸出答題者的智商等級和所屬類別。運行結果如圖所示。
在這裏插入圖片描述
在這裏插入圖片描述
示例代碼如下:

import os


def read_file(path, file_name):
    """
    讀取文件函數
    :param path: 路徑
    :param file_name: 文件名稱
    :return: 文件內容
    """
    # 這裏的/ 也可以換成\\
    with open(path + "/" + file_name, "r", encoding="utf8") as file:
        content = file.read()  # 因爲文件內容比較少 所以直接使用read方法一次性全部讀取
    return content


if __name__ == '__main__':
    print("=======================IQ智力測試=======================")
    print("-" * 55)
    len_que = len(os.listdir("./que"))
    score = 0  # 用來統計得分
    msg = ["智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商2級: 智障",
           "智商3級: 智弱", "智商4級: 臨界", "智商5級: 凡人", "智商6級: 聰慧", "智商7級: 人才", "智商8級: 精英",
           "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才",
           "智商9級: 天才", "智商9級: 天才"]
    # 30道題 每次循環讀取一道題 但是如果增加到了40道題
    # 是不是for循環又要改了 所以最好是動態獲取文件夾中的文件個數
    for i in range(1, len_que + 1):
        # 讀取文件 因爲讀取文件會在後續頻繁使用到 所以封裝爲函數
        print(f"\n第 {i} 題: \n" + read_file("./que", "que" + str(i) + ".txt"))
        # 用戶輸入答案
        user_ans = input("請輸入正確答案前面的數字編號: ").strip()
        # 讀取正確答案
        right_ans = read_file("./ans", "ans" + str(i) + ".txt").strip()
        if user_ans == right_ans:  # 判斷用戶輸入答案與正確一致
            score += 6  # 答案一致加6分
    print(f"你的IQ測試成績爲: {score} {msg[int(score / 10)]}")

5. IQ智商測試與判斷(亂序版)

上面的IQ智商測試程序是按照題目的正常順序出題的,修改程序,隨機抽取題目進行測試,然後根據IQ智商測試分數和表1的9級智商評測表輸出答題者的智商等級和所屬類別。
在這裏插入圖片描述
運行效果如下面的gif圖:
在這裏插入圖片描述

示例代碼如下:

import os
import random


def read_file(path, file_name):
    """
    讀取文件函數
    :param path: 路徑
    :param file_name: 文件名稱
    :return: 文件內容
    """
    # 這裏的/ 也可以換成\\
    with open(path + "/" + file_name, "r", encoding="utf8") as file:
        content = file.read()  # 因爲文件內容比較少 所以直接使用read方法一次性全部讀取
    return content


if __name__ == '__main__':
    print("=======================IQ智力測試=======================")
    print("-" * 55)
    len_que = len(os.listdir("./que"))
    score = 0  # 用來統計得分
    msg = ["智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商2級: 智障",
           "智商3級: 智弱", "智商4級: 臨界", "智商5級: 凡人", "智商6級: 聰慧", "智商7級: 人才", "智商8級: 精英",
           "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才",
           "智商9級: 天才", "智商9級: 天才"]
    num_list = list(range(1, len_que + 1))
    i = 1
    while len(num_list) > 0:
        num = random.choice(num_list)
        num_list.remove(num)
        print(f"\n第 {i} 題: \n" + read_file("./que", "que" + str(num) + ".txt"))
        # 用戶輸入答案
        user_ans = input("請輸入正確答案前面的數字編號: ").strip()
        # 讀取正確答案
        right_ans = read_file("./ans", "ans" + str(num) + ".txt").strip()
        if user_ans == right_ans:  # 判斷用戶輸入答案與正確一致
            score += 6  # 答案一致加6分
        i += 1
    print(f"你的IQ測試成績爲: {score} {msg[int(score / 10)]}")

6. IQ智商測試與判斷(分析版)

修改上面的程序,在對用戶進行智商測試時並將測試成績記錄下來,記錄文件爲源文件同路徑的iq.txt文件。記錄測試成績和智商級別(如天才、精英等),結合表1並分析測試者成績在所有測試成績中的排名與位置,並分析各級智商的分佈情況,運行效果如圖所示。
在這裏插入圖片描述
在這裏插入圖片描述
示例代碼如下:

import os
import random


def read_file(path, file_name):
    """
    讀取文件函數
    :param path: 路徑
    :param file_name: 文件名稱
    :return: 文件內容
    """
    # 這裏的/ 也可以換成\\
    with open(path + "/" + file_name, "r", encoding="utf8") as file:
        content = file.read()  # 因爲文件內容比較少 所以直接使用read方法一次性全部讀取
    return content


if __name__ == '__main__':
    print("=======================IQ智力測試=======================")
    print("-" * 55)
    len_que = len(os.listdir("./que"))
    score = 0  # 用來統計得分
    msg = ["智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商2級: 智障",
           "智商3級: 智弱", "智商4級: 臨界", "智商5級: 凡人", "智商6級: 聰慧", "智商7級: 人才", "智商8級: 精英",
           "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才",
           "智商9級: 天才", "智商9級: 天才"]
    msg2 = ["白癡", "智障", "智弱", "臨界", "凡人", "聰慧", "人才", "精英", "天才"]
    num_list = list(range(1, len_que + 1))
    i = 1
    while len(num_list) > 0:
        num = random.choice(num_list)
        num_list.remove(num)
        print(f"\n第 {i} 題: \n" + read_file("./que", "que" + str(num) + ".txt"))
        # 用戶輸入答案
        user_ans = input("請輸入正確答案前面的數字編號: ").strip()
        # 讀取正確答案
        right_ans = read_file("./ans", "ans" + str(num) + ".txt").strip()
        if user_ans == right_ans:  # 判斷用戶輸入答案與正確一致
            score += 6  # 答案一致加6分
        i += 1
    print(f"你的IQ測試成績爲: {score} {msg[int(score / 10)]}")
    # 將成績和等級寫入文件
    with open("iq.txt", "a", encoding="utf8") as file:
        file.write(str(score) + "," + msg[int(score / 10)].split(":")[1].strip() + "\n")
    # 讀取文件中的測試成績及等級
    score_list = []  # 用來存儲所有的成績
    level_list = []  # 用來存儲所有的等級
    if os.path.exists("iq.txt"):
        with open("iq.txt", "r", encoding="utf8") as file:
            while True:
                line_content = file.readline().strip()
                if line_content == "":
                    break
                else:
                    score_list.append(int(line_content.split(",")[0].strip()))
                    level_list.append(line_content.split(",")[1].strip())
    # 對成績進行排序
    score_list.sort(reverse=True)
    print(f"目前您在所有測試的成績中排名第{score_list.index(score) + 1}名,"
          f"超過了{len(score_list) - (score_list.index(score) + 1)}名選手")
    print("智商測試分析圖: ")
    for item in msg2:
        print(item, int(level_list.count(item)) * chr(9632), level_list.count(item))

7. IQ智商測試與判斷(限時版)

一般的考試都有時間限制,如高考數學、語文的考試時間爲2個小時。爲IQ智商測試添加限時測試功能,要求測試者必須在30分鐘內答題完成測試,每答完一道題輸出相應剩餘時間,如圖1所示。如果答題超時將結束測試,輸出當前的測試成績、IQ級別及測試者在所有測試者中的成績排名和IQ各級智商的分析圖表,程序運行效果如圖2所示。
圖1
在這裏插入圖片描述
示例代碼如下:

import os
import random
import datetime


def read_file(path, file_name):
    """
    讀取文件函數
    :param path: 路徑
    :param file_name: 文件名稱
    :return: 文件內容
    """
    # 這裏的/ 也可以換成\\
    with open(path + "/" + file_name, "r", encoding="utf8") as file:
        content = file.read()  # 因爲文件內容比較少 所以直接使用read方法一次性全部讀取
    return content


if __name__ == '__main__':
    print("=======================IQ智力測試(限時版)=======================")
    print("-" * 55)
    print("說明: 測試時間: 30分鐘,測試題數: 30")
    now = datetime.datetime.now()  # 獲取當前時間
    delay_time = datetime.timedelta(minutes=30)
    stop_time = now + delay_time
    print("測試結束時間爲: ", stop_time.strftime("%Y-%m-%d %H:%M:%S"))
    len_que = len(os.listdir("./que"))
    score = 0  # 用來統計得分
    msg = ["智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商1級: 白癡", "智商2級: 智障",
           "智商3級: 智弱", "智商4級: 臨界", "智商5級: 凡人", "智商6級: 聰慧", "智商7級: 人才", "智商8級: 精英",
           "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才", "智商9級: 天才",
           "智商9級: 天才", "智商9級: 天才"]
    msg2 = ["白癡", "智障", "智弱", "臨界", "凡人", "聰慧", "人才", "精英", "天才"]
    num_list = list(range(1, len_que + 1))
    i = 1
    while len(num_list) > 0:
        num = random.choice(num_list)
        num_list.remove(num)
        print(f"\n第 {i} 題: \n" + read_file("./que", "que" + str(num) + ".txt"))
        # 用戶輸入答案
        user_ans = input("請輸入正確答案前面的數字編號: ").strip()
        # 讀取正確答案
        right_ans = read_file("./ans", "ans" + str(num) + ".txt").strip()
        if user_ans == right_ans:  # 判斷用戶輸入答案與正確一致
            score += 6  # 答案一致加6分
        now = datetime.datetime.now()
        left = int((stop_time - now).seconds / 60)
        if left <= 0:
            print("答題超時,將結束測試!")
            break
        else:
            print(f"剩餘答題時間:{left}分鐘")
        i += 1
    print(f"你的IQ測試成績爲: {score} {msg[int(score / 10)]}")
    # 將成績和等級寫入文件
    with open("iq.txt", "a", encoding="utf8") as file:
        file.write(str(score) + "," + msg[int(score / 10)].split(":")[1].strip() + "\n")
    # 讀取文件中的測試成績及等級
    score_list = []  # 用來存儲所有的成績
    level_list = []  # 用來存儲所有的等級
    if os.path.exists("iq.txt"):
        with open("iq.txt", "r", encoding="utf8") as file:
            while True:
                line_content = file.readline().strip()
                if line_content == "":
                    break
                else:
                    score_list.append(int(line_content.split(",")[0].strip()))
                    level_list.append(line_content.split(",")[1].strip())
    # 對成績進行排序
    score_list.sort(reverse=True)
    print(f"目前您在所有測試的成績中排名第{score_list.index(score) + 1}名,"
          f"超過了{len(score_list) - (score_list.index(score) + 1)}名選手")
    print("智商測試分析圖: ")
    for item in msg2:
        print(item, int(level_list.count(item)) * chr(9632), level_list.count(item))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章