The code you provided has a few syntax errors and indentation issues. Here is the corrected code:

import math

def save_result(operation, result):
    with open("mydata.txt", "a") as file:
        file.write(f"{operation} = {result}\n")

def add(a, b):
    result = a + b
    return result

def subtract(a, b):
    result = a - b
    return result

def multiply(a, b):
    result = a * b
    return result

def divide(a, b):
    result = a / b
    return result

def remainder(a, b):
    result = a % b
    return result

def sine(angle):
    result = math.sin(math.radians(angle))
    return result

def arcsine(value):
    if -1 <= value <= 1:
        result = math.degrees(math.asin(value))
        return result
    else:
        raise ValueError("输入值,必须在-1到1之间 (包-1和1)")

def tangent(angle):
    result = math.tan(math.radians(angle))
    return result

def arctangent(value):
    result = math.degrees(math.atan(value))
    return result

def show_menu():
    print("系统用户界面菜单")
    print("1.运算选项")
    print("2.数据输入")
    print("3.显示结果")
    print("4.保存结果")
    print("5.退出")

def main():
    operation_choice = None
    a = None
    b = None
    result = None
    
    while True:
        show_menu()
        choice = input("请输入您的选择:")
        
        if choice == "1":
            print("请选择运算类型:")
            print("1.加法")
            print("2.减法")
            print("3.乘法")
            print("4.除法")
            print("5.求余")
            print("6.正弦")
            print("7.反正弦")
            print("8.正切")
            print("9.反正切")
            operation_choice = input("请输入您的选择:")
        
        elif choice == "2":
            if operation_choice is not None:
                if operation_choice in ["1", "2", "3", "4", "5"]:
                    a = float(input("请输入第一个数:"))
                    b = float(input("请输入第二个数:"))
                elif operation_choice in ["6", "7", "8", "9"]:
                    a = float(input("请输入一个数值:"))
        
        elif choice == "3":
            if operation_choice is not None and a is not None:
                if operation_choice == "1" and b is not None:
                    result = add(a, b)
                elif operation_choice == "2" and b is not None:
                    result = subtract(a, b)
                elif operation_choice == "3" and b is not None:
                    result = multiply(a, b)
                elif operation_choice == "4" and b is not None:
                    result = divide(a, b)
                elif operation_choice == "5" and b is not None:
                    result = remainder(a, b)
                elif operation_choice == "6":
                    result = sine(a)
                elif operation_choice == "7":
                    try:
                        result = arcsine(a)
                    except ValueError as e:
                        print(e)
                elif operation_choice == "8":
                    result = tangent(a)
                elif operation_choice == "9":
                    result = arctangent(a)
                else:
                    print("无效的选择。")
                    
                if result is not None:
                    print("计算结果:", result)
        
        elif choice == "4":
            if result is not None:
                operation = ""
                if operation_choice == "1":
                    operation = f"{a} + {b}"
                elif operation_choice == "2":
                    operation = f"{a} - {b}"
                elif operation_choice == "3":
                    operation = f"{a} * {b}"
                elif operation_choice == "4":
                    operation = f"{a} / {b}"
                elif operation_choice == "5":
                    operation = f"{a} % {b}"
                elif operation_choice == "6":
                    operation = f"sin({a})"
                elif operation_choice == "7":
                    operation = f"arcsin({a})"
                elif operation_choice == "8":
                    operation = f"tan({a})"
                elif operation_choice == "9":
                    operation = f"arctan({a})"
                    
                save_result(operation, result)
                print("结果已保存到文件。")
            else:
                print("请先进行计算并显示结果。")
        
        elif choice == "5":
            print("退出程序。")
            break
        
        else:
            print("无效的选择。")

if __name__ == "__main__":
    main()

Make sure to save the file with a .py extension and run it using a Python interpreter

import mathdef save_resultoperation result with openmydatatxta as file filewritefoperation = resultndef adda b result =a+b return resultdef subtracta b result =a-b return resultd

原文地址: https://www.cveoy.top/t/topic/hzHz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录