Untitled

 avatar
unknown
python
a year ago
1.1 kB
25
Indexable
def calculate_result(a, op, b):
    if op == '+':
        return a + b
    elif op == '-':
        return a - b
    elif op == '*':
        return a * b
    elif op == '/':
        if b == 0:  # 检查除数是否为0
            return None
        else:
            return round(a / b, 2)  # 除法保留两位小数
    else:
        return None  # 不支持的运算符

def main():
    total_score = 0

    for _ in range(10):
        try:
            num1 = int(input())
            op = input().strip()
            num2 = int(input())
            answer = float(input())

            result = calculate_result(num1, op, num2)

            if result is None:  # 对于不支持的运算或者除数为0的情况
                print("Invalid operation.")
                continue

            if result == answer:
                total_score += 1
        except ValueError:  # 输入的不是整数或合法的浮点数
            print("Invalid input.")
            continue

    print(f"Your final score is: {total_score}")

if __name__ == "__main__":
    main()