[Tutor] Beginner question
Krishnan Shankar
i.am.songoku at gmail.com
Mon Aug 12 19:53:44 CEST 2013
>But in the code there is a flaw. input() will evaluate your user input.
i.e. If you give an integer >expression it will tell the answer. And when
you provide a number it will take it as int type. See >below.
Hi,
Ignore my above statements if using Python 3. Sorry my bad. Had a doubt and
went to the site to see the version of python used. My statement above is
correct only if run in Python 2 and not in 3.
Regards,
Krishnan
On Mon, Aug 12, 2013 at 11:01 PM, Krishnan Shankar
<i.am.songoku at gmail.com>wrote:
> >def checkCave(chosenCave):
> > print('You approach the cave...')
> > time.sleep(2)
> > print('It is dark and spooky...')
> > time.sleep(2)
> > print('A large dragon jumps out in front of you! He opens his jaws
> and...')
> > print()
> > time.sleep(2)
> > friendlyCave = random.randint(1, 2)
> > if chosenCave == str(friendlyCave):
> > print('Gives you his treasure!')
> > else:
> > print('Gobbles you down in one bite!')
> >playAgain = 'yes'
> >while playAgain == 'yes' or playAgain == 'y':
> > displayIntro()
> > caveNumber = chooseCave()
> > checkCave(caveNumber)
> > print('Do you want to play again? (yes or no)')
> > playAgain = input()
>
> Hi,
>
> - Here we are passing the chosen integer (1 or 2) got from chooseCave()
> method to checkCave as arguement
> - When called in while loop inside checkCave the following happens;
> - The statements of approaching the cave and seeing the dragon are
> printed with a time interval of 2 secs between each
> - Randomly either 1 or 2 is generated by the randint() method of
> random module in python.
> - That randomly generated integer (1 0r 2) is compared with our
> integer input (1 or 2)
> - If they match dragon gives us gold. Or else
> - We will be eaten by dragon :)
>
> But in the code there is a flaw. input() will evaluate your user input.
> i.e. If you give an integer expression it will tell the answer. And when
> you provide a number it will take it as int type. See below.
>
> >>> var = input()
> 1+2+3
> >>> var
> 6
> >>> var = input()
> 2
> >>> type(var)
> <type 'int'>
> >>> var = input()
> s
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<string>", line 1, in <module>
> NameError: name 's' is not defined
> >>> var = input()
>
> So since the integer number is checked with string in Line 13, it will run
> into infinite loop. If you use raw_input() instead of input() you will be
> able to run the example.
>
> Regards,
> Krishnan
>
>
> On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney <cybervigilante at gmail.com>wrote:
>
>> On 12 August 2013 02:14, Karim Liateni <kliateni at gmail.com> wrote:
>>
>>> 5ÿt5ÿ6hhhyyyfrrtr
>>>
>>> eschneider92 at comcast.net a écrit :
>>>
>>> >I've been learning python from the website 'inventwithpython.com', and
>>> I'm on a chapter that covers the following code:
>>>
>>
>> Just a quick note - not on the algorithm itself. If you run that in some
>> IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the
>> prints will then print at once with no delay ;') If that happens, run it
>> from the command line or try a different IDE.
>>
>> Jim
>> --
>>
>> "If you don't know it's impossible, it's easier to do." --Neil Gaiman
>> "The Process is not the Picture...Reality can only be proved to be
>> weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup
>> "You cannot use logic to justify logic, so logic itself has no basis
>> other than faith." --Agrippa
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130812/f6527afb/attachment.html>
More information about the Tutor
mailing list