gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:07:51 26 4
gpt4 key购买 nike

我已经为一个项目编写了代码,我正在尝试将最终输出(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

最佳答案

试试这个

with open('exported.txt', 'w') as f:
f.write('{0} {1}'.format(a, b))

它的作用是打开文件,然后向其中写入一个字符串。

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

关于python - 将变量值导出到单独的 (.txt) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48892101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com