loops

Verde Denim tdldev at gmail.com
Sun Dec 2 17:28:48 EST 2012


On 12/02/2012 04:43 PM, Mitya Sirenef wrote:
> On 12/02/2012 04:39 PM, Verde Denim wrote:
>> I'm just getting into py coding, and have come across an oddity in a py
>> book - while loops that don't work as expected...
>>
>> import random
>>
>> MIN = 1
>> MAX = 6
>>
>> def main():
>>      again = 'y'
>>
>>      while again == 'y':
>>          print('Rolling...')
>>          print('Values are: ')
>>          print(random.randint(MIN, MAX))
>>          print(random.randint(MIN, MAX))
>>
>>          again = input('Roll again? (y = yes): ')
>>
>> main()
>>
>> Produces -
>> python dice_roll.py
>> Rolling...
>> Values are:
>> 5
>> 4
>> 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...
> 
> I believe that should be raw_input, not input . input() evaluates user's
> input
> in local scope.  -m

m
Nicely done! That fixed it! Is that a version feature or should I take
what I find in these books with a grain of salt?

-j



More information about the Python-list mailing list