Message Boards Message Boards

GROUPS:

[?] Use functions in WriteLine?

Posted 1 year ago
2016 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

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 + 5

StringForm 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 =

Thank you very much and wonderful, it was helpful. :)

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract