MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: newbe question about Sting Comparison

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107043] Re: [mg107010] newbe question about Sting Comparison
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 1 Feb 2010 06:13:07 -0500 (EST)
  • Reply-to: hanlonr at cox.net

The response to Input is interpreted as a Mathematica expression; not as a string unless you enter a string (using quotes).  In your example, you entered the symbol yes and tested whether the symbol was equal to the string "yes". This was neither true nor false and you did not indicate what to do if uncertain (fourth argument to If]. Consequently, either test for the symbol yes or convert the input to a string prior to the test for "yes".

playAGame[] := Module[{car},
  ans = Input["Would you like to play a game"];
  Print[ans];
  If[ans == "yes", "you answered yes",
   "you did not answer yes", "I don't know"]]

playAGame[]

yes

I don't know

playAGame[] := Module[{car},
  ans = Input["Would you like to play a game"];
  Print[ans];
  If[ans == yes, "you answered yes",
   "you did not answer yes", "I don't know"]]

playAGame[]

yes

you answered yes

playAGame[] := Module[{car},
  ans = ToString[Input["Would you like to play a game"]];
  Print[ans];
  If[ans == "yes", "you answered yes",
   "you did not answer yes", "I don't know"]]

playAGame[]

yes

you answered yes


Bob Hanlon

---- "Biff. Bartolemew" <harrytuttle777 at mac.com> wrote: 

=============
Hello,

I hope someone can help me out, and not laugh to hard at my stupidity, 
but, I am trying write a little program


playAGame[] := Module[
     {car},
     ans = Input["Would you like to play a game"];
     Print[ans];
     If[ans == "yes", Print["you answered yes"],
         Print["you did not answer yes"]
     ];
     If[5 == 5, Print["You suck"], Print["You Rock"]];

     ]

The first If statement does not seem to be evaluated.

Yet, if I type:
   "yes"=="yes",   I get the answer I am looking for.

It seems like the string comparison is acting different in the middle of 
the If statement.  Can someone tell me what I am doing wrong?


-Thanks




  • Prev by Date: Re: Partition prime list into equal k sublists. How to
  • Next by Date: Re: Using variable name as string AND value ?
  • Previous by thread: Re: newbe question about Sting Comparison
  • Next by thread: Evaluating Problem in Generating Random Text