Python 将变量值导出到单独的(.txt)文件

Python 将变量值导出到单独的(.txt)文件,python,Python,我已经为一个项目编写了代码,我正在尝试将最终输出(a/b)输出到外部文本文档。我尝试了很多,但目前没有任何效果 #SUITABLE GREETING FOR THE USER print ("Hi Jim, welcome to the bear factory wage calculator program.") #INPUT bears made bears_made = int(input("Enter the number of bears you made: ")) #INPUT

我已经为一个项目编写了代码,我正在尝试将最终输出(a/b)输出到外部文本文档。我尝试了很多,但目前没有任何效果

#SUITABLE GREETING FOR THE USER
print ("Hi Jim, welcome to the bear factory wage calculator program.")

#INPUT bears made
bears_made = int(input("Enter the number of bears you made: "))

#INPUT hours worked
hours_worked = int(input("Enter the number of hours you worked: "))

#ABILITY TO CHANGE THE WAGE PER BEAR AND HOUR
z = str(input("Would you like to change the wage? "))
if z =="yes":
    x = int(input("Enter how much the user should get per bear: "))
    a = bears_made*(x)
    e = int(input("Enter how much the user should get an hour: "))
    b = hours_worked*(e)

else:
    #BEARSMADE x 2 = a
    a = (bears_made *2)

    #HOURSWORKED x 5 = b
    b = (hours_worked *5)


#OUTPUT BIGGER VALUE
if a > b:
    #OUTPUT A
    print ("You earned", a," pounds")
else:
    #OUTPUT B
    print ("You earned", b," pounds")

#f = open("exported.txt",'w') ###This is the area trying to troubleshoot
#text = (a) (b)         
#f.write (text)
#f.close()

#END OF PROGRAM
编辑:对不起,我刚刚意识到我应该添加更多。我得到以下错误:

Traceback (most recent call last):
  File "C:\Users\samho\OneDrive\Desktop\plannedcode.py", line 37, in <module>
    text = (a) (b)
TypeError: 'int' object is not callable
回溯(最近一次呼叫最后一次):
文件“C:\Users\samho\OneDrive\Desktop\plannedcode.py”,第37行,在
正文=(a)(b)
TypeError:“int”对象不可调用

这就是您想要做的。希望能有帮助

#SUITABLE GREETING FOR THE USER
print ("Hi Jim, welcome to the bear factory wage calculator program.")

#INPUT bears made
bears_made = int(input("Enter the number of bears you made: "))

#INPUT hours worked
hours_worked = int(input("Enter the number of hours you worked: "))

#ABILITY TO CHANGE THE WAGE PER BEAR AND HOUR
z = str(input("Would you like to change the wage? "))
if z =="yes":
    x = int(input("Enter how much the user should get per bear: "))
    a = bears_made*(x)
    e = int(input("Enter how much the user should get an hour: "))
    b = hours_worked*(e)

else:
    #BEARSMADE x 2 = a
    a = (bears_made *2)

    #HOURSWORKED x 5 = b
    b = (hours_worked *5)


#OUTPUT BIGGER VALUE
if a > b:
    #OUTPUT A
    print ("You earned", a," pounds")
else:
    #OUTPUT B
    print ("You earned", b," pounds")

f = open("exported.txt",'w') ###This is the area trying to troubleshoot
text = str(a)+" "+str(b)
f.write (text)
f.close()

#END OF PROGRAM

在代码中,将值分配给
a
b
,这两个变量中都有数字,并且类型为
int

当你跑步时
text=(a)(b)

您实际上调用的是:
1(2)
,这是无效的

我假设您希望将这两个语句都输出到文本文件中,因此将该语句替换为:
text=“({})({})”格式(a,b)

试试这个

with open('exported.txt', 'w') as f:
    f.write('{0} {1}'.format(a, b))
这样做的目的是打开文件,然后向其中写入字符串


字符串将是两个数字a和b

请用a展开“当前没有任何东西在工作”。什么是
text=(a)(b)
打算做什么?我们知道您没有经验,但我们仍然需要您的问题的某些方面。特别是,准确描述哪些不起作用。你期望得到什么样的输出,你得到了什么样的输出,区别是什么?我只是对我得到的错误进行了一点扩展。我正在尝试将变量a和b导出到外部.txt文档中。谢谢,这样可以将数字复制到.txt文档中。你知道我如何格式化它吗?我在文件上得到了一堆随机小数(即,输入'20'和'10'后,文本文档上会打印出0.8。感谢您给我一个示例,说明您在输入what时希望输出什么?如果答案满足您的第一个问题,请接受答案。当您给出20和10,然后是否时,根据您的程序,文本文件中打印的内容是-(20*2)/(10*5)=0.8,这是正确的。对不起,我的坏-应该更具体一些。我希望它将值A和值B输出到.txt文档中。我现在不关心格式,但我只是尝试将两个单独的值A和B输入到文本文档中。@SamHockham进行了更改。请检查代码并接受它,如果它满足您的要求你的标准。当我这样做时,它会将它打印到文本文档:({})({})。格式(a,b)谢谢,这很好。还有一个问题,对不起,lol,你知道如何使代码输出仅为两个值中的较大值吗?我不能用一个基本的IF和ELSE语句吗?当然,python有一个max函数-将write改为:
f.write('{0}“.格式(最大(a,b)))