Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: loops

Reply
Thread Tools

Re: loops

 
 
Chris Angelico
Guest
Posts: n/a
 
      12-02-2012
On Mon, Dec 3, 2012 at 8:39 AM, Verde Denim <(E-Mail Removed)> wrote:
> again = input('Roll again? (y = yes): ')
>
> Roll again? (y = yes): y
> Traceback (most recent call last):
> File "dice_roll.py", line 17, in <module>
> main()
> File "dice_roll.py", line 15, in main
> again = input('Roll again? (y = yes): ')
> File "<string>", line 1, in <module>
> NameError: name 'y' is not defined
>
> This same loop structure appears in many places in this book "Starting
> out with Python, 2nd ed, Tony Gaddis), and they all yield the same
> error. Is there something I'm missing here?
>
> Thanks for the input...


The loop isn't the problem, your input() function is. In Python 2,
input() did the strange thing of evaluating what you typed; to simply
ask for a line and return it as a string, use raw_input(). As of
Python 3, the input() function does what raw_input() used to do, which
is what you want here.

You may be able to fix the problem by simply changing the function
name, but if the book is assuming Python 3, you will probably want to
switch to the newer interpreter. Also, the newer Pythons have a large
number of improvements in other areas, so it's well worth switching
anyway.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple For Loops? pinod01@sympatico.ca VHDL 1 02-22-2006 03:10 PM
Etherchannel disabled = spanning tree loops... traust Cisco 0 02-21-2006 05:34 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Perl loops should use break, not last Jeremy Morton Perl 1 01-30-2005 10:50 PM
to many FOR loops? eismaus4 VHDL 1 04-27-2004 02:54 PM



Advertisments