Hello, I am trying to make a program that takes input on the command-line and returns the result to textfile.
The problem is that I get the error

TypeError: can only concatenate tuple (not "str") to tuple
WARNING: Failure executing file: <write_cml_function.py>

from scitools.all import *
# imports numpy and StringFunction(turns f into python 
# function)
import sys

f = sys.argv[1] # function
f = StringFunction(f)
a = int(sys.argv[2]) # left bounder
b = int(sys.argv[3]) # right bounder
n = int(sys.argv[4])

x = linspace(a,b,n)
resultat = f(x)


s = open("resultat.txt","w")
for i in range(0,n,1):
    line = x[i], resultat
    s.write(line + "\n")
s.close()

Recommended Answers

All 4 Replies

I don't have scitools, so I can't test this code, but why don't you try changing the line:

line = x[i], resultat

to

line = str( x[i] ), str( resultat )

Tell me if that makes a difference!

Writing line = x[i], resultat defines line as the pair (x[i], resultat) , whihc is a tuple and not a string. Python complains with line+ "\n" , which adds a tuple and a string. You could write "%s\n" % line if you want to see the tuple in your output file. Otherwise you could define line = "%s, %s" % (x[i], resultat) for example.

Your error sits right here ...

.....
s = open("resultat.txt","w")
for i in range(0,n,1):
    line = x[i], resultat
    s.write(line + "\n")
s.close()

This
line = x, resultat
will give you a tuple, so
line + "\n"
will give you the error

Try:
s.write(str(line) + "\n")

Thank you for the fast replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.