Python 检索文本文件中的最后一个值,并允许用户重新使用它来创建新的cal

Python 检索文本文件中的最后一个值,并允许用户重新使用它来创建新的cal,python,Python,我正在尝试检索存储用户所有计算的文本文件中的最后一个值,用户将能够在新计算中再次使用文本文件中的最后一个值,但当涉及到该部分时,我不确定如何使其工作。谢谢 question=input(“键入yes以重复使用您的计算!:”) 运算符=输入(“请选择运算符:”) num2=浮点(输入(“请选择第二个数字:”) 尝试: 如果有疑问,请回答“是”: 打开(“file.txt”、“r”)作为t: x=t.readlines()[-1].split() 打印(x[-1]) t、 关闭() 除: 打印(“那

我正在尝试检索存储用户所有计算的文本文件中的最后一个值,用户将能够在新计算中再次使用文本文件中的最后一个值,但当涉及到该部分时,我不确定如何使其工作。谢谢

question=input(“键入yes以重复使用您的计算!:”)
运算符=输入(“请选择运算符:”)
num2=浮点(输入(“请选择第二个数字:”)
尝试:
如果有疑问,请回答“是”:
打开(“file.txt”、“r”)作为t:
x=t.readlines()[-1].split()
打印(x[-1])
t、 关闭()
除:
打印(“那是错误的”)
最后:
打印(“祝您有美好的一天”)
示例:200+200=400,400+200=600 用户重复使用最后一个值,然后系统会提示用户输入第二个数字,并提示操作员创建新答案

600.0 + 400.0 = 1000.0
1000.0 + 200.0 = 1200.0
500.0 + 300.0 = 800.0

file.txt看起来像这样

您可以使用
eval
从字符串计算值。可以找到
eval
内置函数的详细信息

假设最初我们有一个包含以下内容的文件
file.txt

200+200 = 400
我们可以执行以下步骤:

  • 从用户处获取新的运算符和操作数
  • 从文件中读取最后一个值
  • 使用用户输入、运算符创建字符串
  • 使用
    eval
    函数从字符串计算新值
  • 使用用户输入、运算符和计算值创建新行
  • 最后,我们可以将这一新行附加到现有的
    file.txt
    文件中
  • code.py

    question = input("type yes to reuse your calculation !: ")
    operator = input("please choose a operator: ")
    num2 = input("please choose second number: ")
    try:
        if "yes" in question:
            old_value = None
            with open("file.txt", "r") as t:
                x = t.readlines()[-1].split()
                old_value = x[-1].strip()
                t.close()
            if old_value:
                new_operation = old_value + operator + num2
                new_value = eval(new_operation)
                new_line = new_operation +" = "+ str(new_value)+"\n"
                print(new_line)
                with open("file.txt", "a") as myfile:
                    myfile.write(new_line)
    except Exception as e:
        print(str(e))
        print("thats wrong")
    finally:
        print("Have a great day")
    
    运行代码:

    type yes to reuse your calculation !: yes
    please choose a operator: -
    please choose second number: 100
    400-100 = 300
    
    Have a great day
    
    更新的
    file.txt

    200+200 = 400
    400-100 = 300
    

    您可以使用
    eval
    从字符串计算值。可以找到
    eval
    内置函数的详细信息

    假设最初我们有一个包含以下内容的文件
    file.txt

    200+200 = 400
    
    我们可以执行以下步骤:

  • 从用户处获取新的运算符和操作数
  • 从文件中读取最后一个值
  • 使用用户输入、运算符创建字符串
  • 使用
    eval
    函数从字符串计算新值
  • 使用用户输入、运算符和计算值创建新行
  • 最后,我们可以将这一新行附加到现有的
    file.txt
    文件中
  • code.py

    question = input("type yes to reuse your calculation !: ")
    operator = input("please choose a operator: ")
    num2 = input("please choose second number: ")
    try:
        if "yes" in question:
            old_value = None
            with open("file.txt", "r") as t:
                x = t.readlines()[-1].split()
                old_value = x[-1].strip()
                t.close()
            if old_value:
                new_operation = old_value + operator + num2
                new_value = eval(new_operation)
                new_line = new_operation +" = "+ str(new_value)+"\n"
                print(new_line)
                with open("file.txt", "a") as myfile:
                    myfile.write(new_line)
    except Exception as e:
        print(str(e))
        print("thats wrong")
    finally:
        print("Have a great day")
    
    运行代码:

    type yes to reuse your calculation !: yes
    please choose a operator: -
    please choose second number: 100
    400-100 = 300
    
    Have a great day
    
    更新的
    file.txt

    200+200 = 400
    400-100 = 300
    

    文件是什么样子的?…还有,你能提供一个用户输入的示例和你想要的结果吗?文件是什么样子的?…还有,你能提供一个用户输入的示例和你想要的结果吗?