Sam Hockham
Sam Hockham

Reputation: 23

Exporting variable values to a separate (.txt) file

I have made code for a project and I am trying to get the final output (a/b) to be outputted to an external text document. I have tried a lot, but nothing is currently working.

#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

Edit : Sorry, just realised I should add more. I am getting the following error:

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

Upvotes: 1

Views: 83

Answers (3)

Rafael Barros
Rafael Barros

Reputation: 2881

In your code, you assign values to a and b, these two variables have numbers in them, and are of type int.

When you run
text = (a)(b)
You're essentially calling this: 1(2), which is invalid.

I assume you want to output both of those to the text file so replace that statement with this:
text = "({})({})".format(a, b)

Upvotes: 1

Abhisek Roy
Abhisek Roy

Reputation: 584

This does what you are trying to do. Hope it helps.

#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

Upvotes: 0

Steve
Steve

Reputation: 989

try this

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

What that does is open the file, then write a string to it.

The string will be the two numbers a and b

Upvotes: 1

Related Questions