# [?] Use functions in WriteLine?

Posted 2 years ago
2682 Views
|
3 Replies
|
5 Total Likes
|
 Hi, I'm new with Mathematica and I'm trying to make thirty individual exams assignment for my students. My problem is: I need to change a number or one word in a string inside WriteLine, but Mathematica does not like it. WriteLine[file,"Some text that is a question like Solve "RandomChoice[{1,2,3,4,5}]"+"RandomChoice[{1,2,3,4,5}]] I also tried: WriteLine[file,"Some text that is a question like Solve ",RandomChoice[{1,2,3,4,5}],"+",RandomChoice[{1,2,3,4,5}]] WriteLine[file,"Some text that is a question like Solve ";RandomChoice[{1,2,3,4,5}];"+";RandomChoice[{1,2,3,4,5}]] Nothing works :/Expected out is Some text that is a question like Solve 4+2
3 Replies
Sort By:
Posted 2 years ago
 Try this: text = "Some text that is a question like Solve " <> ToString@RandomChoice[{1, 2, 3, 4, 5}] <> "+" <> ToString@RandomChoice[{1, 2, 3, 4, 5}]; WriteLine[file, text] 
 WriteLine write each argument after the first on a new line in the file: WriteLine[file, "Some text that is a question like Solve", RandomChoice[{1, 2, 3, 4, 5}], "+", RandomChoice[{1, 2, 3, 4, 5}]] FilePrint["testQuestion.txt"] Some text that is a question like Solve 3 + 5StringForm is an easy way to get all the arguments composed into a single String: StringForm["Solve  +  =", RandomChoice[{1, 2, 3, 4, 5}], RandomChoice[{1, 2, 3, 4, 5}]] Solve 4 + 3 =Putting it all together (after closing and deleting the file): Close[file]; DeleteFile[file] file = File[CreateFile["testQuestion.txt"]]; WriteLine[file, StringForm["Solve  +  =", RandomChoice[{1, 2, 3, 4, 5}], RandomChoice[{1, 2, 3, 4, 5}]]] FilePrint[file] Solve 2 + 5 =