Re: [Tutor] Virtualenv Error

2011-04-24 Thread Jonathan Zacsh
On Sun, Apr 24, 2011 at 22:52, Malcolm Newsome
 wrote:
> Hey All,
>
>
>
> Does anyone have any experience creating virtualenvs that can assist me with
> this error.  I’m running windows 7, python 2.7.1 and virtualenv 1.6.
>
>
>
> I installed virtualenv using easy_install and there were no errors.  I get
> the following error when trying to create a virtualenv.
>
>
>
> Any assistance is appreciated! Thanks!
>
>
>
>
>
> Microsoft Windows [Version 6.1.7601]
>
> Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
>
>
>
> C:\Users\Malcolm>virtualenv malcery
>
> New python executable in malcery\Scripts\python.exe
>
> Installing setuptoolsdone.
>
>   Complete output from command C:\Users\Malcolm\malcery\Scripts\python.exe
> C:\Users\Malcolm\mal...sy_install-script.py
> C:\Users\Malcolm\Pyt...pport\pip-1.0.tar.gz:
>
>   Traceback (most recent call last):
>
>   File "C:\Users\Malcolm\malcery\Scripts\easy_install-script.py", line 8, in
> 
>
>     load_entry_point('setuptools==0.6c11', 'console_scripts',
> 'easy_install')()
>
>   File
> "C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py",
> line 318, in load_entry_point
>
>     return get_distribution(dist).load_entry_point(group, name)
>
>   File
> "C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py",
> line 2221, in load_entry_point
>
>     return ep.load()
>
>   File
> "C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py",
> line 1954, in load
>
>     entry = __import__(self.module_name, globals(),globals(), ['__name__'])
>
>   File
> "C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py",
> line 21, in 
>
>
>
>   File
> "C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\package_index.py",
> line 2, in 
>
>   File "C:\Users\Malcolm\Python27\lib\urllib2.py", line 94, in 
>
>     import httplib
>
>   File "C:\Users\Malcolm\Python27\lib\httplib.py", line 71, in 
>
>     import socket
>
>   File "C:\Users\Malcolm\Python27\lib\socket.py", line 47, in 
>
>     import _socket
>
> ImportError: No module named _socket
>
> 
>
> Traceback (most recent call last):
>
>   File "C:\Users\Malcolm\Python27\Scripts\virtualenv-script.py", line 8, in
> 
>
>     load_entry_point('virtualenv==1.6', 'console_scripts', 'virtualenv')()
>
>   File
> "C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtualenv.py",
> line 745, in main
>
>     prompt=options.prompt)
>
>   File
> "C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtualenv.py",
> line 845, in create_environment
>
>     install_pip(py_executable)
>
>   File
> "C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtualenv.py",
> line 602, in install_pip
>
>     filter_stdout=_filter_setup)
>
>   File
> "C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtualenv.py",
> line 813, in call_subprocess
>
>     % (cmd_desc, proc.returncode))
>
> OSError: Command C:\Users\Malcolm\malcery\Scripts\python.exe
> C:\Users\Malcolm\mal...sy_install-script.py
> C:\Users\Malcolm\Pyt...pport\pip-1.0.tar.gz failed with error code 1
>
>
>
> C:\Users\Malcolm>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Have you tried using pip instead of easy_install? Its supposed to the
"newer, better" approach.
 http://www.pip-installer.org/

I don't know that it matters. I'm new to python myself.

-- 
Jonathan Zacsh
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Virtualenv Error

2011-04-24 Thread Malcolm Newsome
Hey All,

 

Does anyone have any experience creating virtualenvs that can assist me with
this error.  I'm running windows 7, python 2.7.1 and virtualenv 1.6.

 

I installed virtualenv using easy_install and there were no errors.  I get
the following error when trying to create a virtualenv.  

 

Any assistance is appreciated! Thanks!

 

 

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

 

C:\Users\Malcolm>virtualenv malcery

New python executable in malcery\Scripts\python.exe

Installing setuptoolsdone.

  Complete output from command C:\Users\Malcolm\malcery\Scripts\python.exe
C:\Users\Malcolm\mal...sy_install-script.py
C:\Users\Malcolm\Pyt...pport\pip-1.0.tar.gz:

  Traceback (most recent call last):

  File "C:\Users\Malcolm\malcery\Scripts\easy_install-script.py", line 8, in


load_entry_point('setuptools==0.6c11', 'console_scripts',
'easy_install')()

  File
"C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_
resources.py", line 318, in load_entry_point

return get_distribution(dist).load_entry_point(group, name)

  File
"C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_
resources.py", line 2221, in load_entry_point

return ep.load()

  File
"C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_
resources.py", line 1954, in load

entry = __import__(self.module_name, globals(),globals(), ['__name__'])

  File
"C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\setu
ptools\command\easy_install.py", line 21, in 

 

  File
"C:\Users\Malcolm\malcery\lib\site-packages\setuptools-0.6c11-py2.7.egg\setu
ptools\package_index.py", line 2, in 

  File "C:\Users\Malcolm\Python27\lib\urllib2.py", line 94, in 

import httplib

  File "C:\Users\Malcolm\Python27\lib\httplib.py", line 71, in 

import socket

  File "C:\Users\Malcolm\Python27\lib\socket.py", line 47, in 

import _socket

ImportError: No module named _socket



Traceback (most recent call last):

  File "C:\Users\Malcolm\Python27\Scripts\virtualenv-script.py", line 8, in


load_entry_point('virtualenv==1.6', 'console_scripts', 'virtualenv')()

  File
"C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtua
lenv.py", line 745, in main

prompt=options.prompt)

  File
"C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtua
lenv.py", line 845, in create_environment

install_pip(py_executable)

  File
"C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtua
lenv.py", line 602, in install_pip

filter_stdout=_filter_setup)

  File
"C:\Users\Malcolm\Python27\lib\site-packages\virtualenv-1.6-py2.7.egg\virtua
lenv.py", line 813, in call_subprocess

% (cmd_desc, proc.returncode))

OSError: Command C:\Users\Malcolm\malcery\Scripts\python.exe
C:\Users\Malcolm\mal...sy_install-script.py
C:\Users\Malcolm\Pyt...pport\pip-1.0.tar.gz failed with error code 1

 

C:\Users\Malcolm>

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assistance

2011-04-24 Thread Alan Gauld

"Krystal Brosz"  wrote

i'm struggling with a program, i feel like i am really close to 
getting it


You are not far away but you have a few little
problems to fix.

but i cannot find a way to use the target variables inside of a 
loop:


I have no idea what you mean by "target variables".
There are no variables in the code called 'target' and
I don't understand what target you mean?


def main():
   gradesEntered = 0
   score = 0
   numberOfGrades = 0
   numberOfGrades = int(raw_input("Please enter the number of 
grades:" ))


#Begin a 'for' loop to enter each score


The comment is confusing because you actually enter a while loop.
Inaccurate comments are worse than no comments at all!


   while numberOfGrades != gradesEntered:
   grade = int(raw_input("Please enter the grade:" ))
   gradesEntered += 1
   score =+ grade


Note that += and =+ do different things. I suspect this last line is
not doing what you think. Details like this are very important in
programming, especially since both forms are valid code, they
just do different things!


   grade = [numberOfGrades]


And I don't know what you think this is doing but it is in fact
overwriting the value of grade that the user entered with a list
of one element, numberOfGrades. You then throw this away
the next time round the loop.


   for i in range (numberOfGrades):
   grade.append(i)
   print determine_grade(grade,i)


Now you append i to the list containing numberOfGrades

So if the user said they would have 3 grades you wind up
with grade being equal to

[3,0,1,2]

Which is almost certainly not what you want?
You also pass this list to determine_grade which
expects a number rather than a list so will always
print an error I suspect. Although I'd also expect it to
throw an NameError exception since result will not be defined...
Are you sure this is the actual code you are running?


#get the grade letter for scores
   print "the total number of grades entered are:", score
   print "The average of the scores is:", calc_average
   print "The number of grades entered is:", gradesEntered


So ths prints the value of yourt last grade multiplied by  +1
followed by a calc_average which is not actually set
anwthere in your code. It then prints gradesEntered
which should be the same as numberOfGrades.


def determine_grade(grade,i):


I'm not sure why you have i in the parameter list?
You don't do anything with it.


   if grade >= 90 and grade <= 100:
 result = 'Your grade is A'
   elif grade >=80 and grade <= 89:
 result = 'Your grade is B'
   elif grade >= 70 and grade <= 79:
 result = 'Your grade is C'
   elif grade >=60 and grade <= 69:
 result = 'Your grade is D'
   elif grade < 60:
 result = 'Your grade is F'
   else:
   print 'Error: Invalid grade.'
   return result


If you reach the last 'else', result will not have been
assigned and yet, you try to return it. I'd expect
that to throw an error.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Assistance

2011-04-24 Thread Krystal Brosz
Hi there,

i'm struggling with a program, i feel like i am really close to getting it
but i cannot find a way to use the target variables inside of a loop:
I'm trying to get the program to ask the user, how many grades are you going
to enter.  Then i want to work out the average which is fine.  But then i
want the loop to print out from each grade entered the letter grade as per
my if statement.  All it does is print the last grade out a certain number
of times. Is this even possible or am i overthinking it?

Some code is:
def main():

gradesEntered = 0
score = 0
numberOfGrades = 0
#get the number of grades being checked
numberOfGrades = int(raw_input("Please enter the number of grades:" ))

#Begin a 'for' loop to enter each score
while numberOfGrades != gradesEntered:
grade = int(raw_input("Please enter the grade:" ))
gradesEntered += 1
score =+ grade

grade = [numberOfGrades]
for i in range (numberOfGrades):
grade.append(i)
print determine_grade(grade,i)

#get the grade letter for scores
print "the total number of grades entered are:", score
print "The average of the scores is:", calc_average
print "The number of grades entered is:", gradesEntered


def determine_grade(grade,i):
if grade >= 90 and grade <= 100:
  result = 'Your grade is A'
elif grade >=80 and grade <= 89:
  result = 'Your grade is B'
elif grade >= 70 and grade <= 79:
  result = 'Your grade is C'
elif grade >=60 and grade <= 69:
  result = 'Your grade is D'
elif grade < 60:
  result = 'Your grade is F'
else:
print 'Error: Invalid grade.'
return result

main()

Thank you in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to solve this problem in python?

2011-04-24 Thread Modulok
Ratna,

This list doesn't usually do your homework. However, we will help you with key
issues. What have you tried already? Where are you stuck? Is there a certain
aspect you don't understand? (Questions like these get lots of help around
here. Informally saying "do my homework" generally doesn't.)

-Modulok-

On 4/24/11, Ratna Banjara  wrote:
>  Write a function named countRepresentations that returns the number
> of ways that an amount of money in rupees can be represented as rupee
> notes. For this problem we only use  rupee notes in denominations of
> 1, 2, 5, 10 and 20 rupee notes.
>
> The signature of the function is:
>def countRepresentations(int numRupees)
>
> For example, countRepresentations(12) should return 15 because 12
> rupees can be represented in the following 15 ways.
>   1. 12 one rupee notes
>   2. 1 two rupee note plus 10 one rupee notes
>   3. 2 two rupee notes plus 8 one rupee notes
>   4. 3 two rupee notes plus 6 one rupee notes
>   5. 4 two rupee notes plus 4 one rupee notes
>   6. 5 two rupee notes plus 2 one rupee notes
>   7. 6 two rupee notes
>   8. 1 five rupee note plus 7 one rupee notes
>   9. 1 five rupee note, 1 two rupee note and 5 one rupee notes
>   10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes
>   11. 1 five rupee note, 3 two notes and 1 one rupee note
>   12. 2 five rupee notes and 2 one rupee notes
>   13. 2 five rupee notes and 1 two rupee note
>   14. 1 ten rupee note and 2 one rupee notes
>   15. 1 ten rupee note and 1 two rupee note
>
> Hint: Use a nested loop that looks like this. Please fill in the
> blanks intelligently, i.e. minimize the number of times that the if
> statement is executed.
> for (int rupee20=0; rupee20<=__; rupee20++)
>for (int rupee10=0; rupee10<=__; rupee10++)
>   for (int rupee5=0; rupee5<=__; rupee5++)
>  for (int rupee2=0; rupee2<=__; rupee2++)
> for (int rupee1=0;  rupee1<=__; rupee1++)
> {
> if (___)
>count++
> }
>
> --
> Regards,
> Ratna P Banjara
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Assistance

2011-04-24 Thread Krystal Brosz
Hi Steve, thank you so much for your prompt reply.  sorry i'm sending
another email as i haven't learnt how to replly to the posts properly yet.

I've had a go at implementing a list, but all i get is: It looks as though
it is appending all three values together rather than seperatly..
Please enter the number of grades:3
Please enter the grade:60
Please enter the grade:90
Please enter the grade:50
[60, 90, 50]
[60, 90, 50]
[60, 90, 50]

I have added this code:
Initialised the list under def main() grades = []

added under the while loop:
grades.append(grade)


This is now my for statement
for i in range (numberOfGrades):
determine_grade(grade, i)
print grades

I feel so close yet so far on this one!  I've changed variable, i don't
think its appending to the list correctly?
Should i be placing the grades.append(grade) under def main()?  Although it
needs to add to the list after each iteration
any advice is appreciated.

Thanks again,
Krystal
:)



Krystal Brosz wrote:
> Hi there,> > i'm struggling with a program, i feel like i am really close
to getting it> but i cannot find a way to use the target variables inside of
a loop:> I'm trying to get the program to ask the user, how many grades are
you going> to enter.  Then i want to work out the average which is fine.
But then i> want the loop to print out from each grade entered the letter
grade as per> my if statement.  All it does is print the last grade out a
certain number> of times. Is this even possible or am i overthinking it?

No, you're not over-thinking it, but you do have a few small errors in
your code:


> Some code is:> def main():> > gradesEntered = 0> score = 0>
numberOfGrades = 0> #get the number of grades being checked>
numberOfGrades = int(raw_input("Please enter the number of grades:" ))> >
#Begin a 'for' loop to enter each score> while numberOfGrades !=
gradesEntered:> grade = int(raw_input("Please enter the
grade:" ))>
gradesEntered += 1> score =+ grade> grade = [numberOfGrades]

Here you ask the user to enter a grade. Suppose they enter (say) 75.
Your program will store 75 in grade, and then a moment later over-write
that by storing [numberOfGrades] in grade.

I believe that what you need is to have a variable "grades" (note
plural), and each time around the while loop, you need to append the
current grade to the grades list.

Then, after you have collected all the grades, you can iterate over the
list to get each grade one at a time:

for grade in grades:
 print grade


See how you go with that, and don't hesitate to ask if anything is unclear!





-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Assistance

2011-04-24 Thread Steven D'Aprano

Krystal Brosz wrote:

Hi there,

i'm struggling with a program, i feel like i am really close to getting it
but i cannot find a way to use the target variables inside of a loop:
I'm trying to get the program to ask the user, how many grades are you going
to enter.  Then i want to work out the average which is fine.  But then i
want the loop to print out from each grade entered the letter grade as per
my if statement.  All it does is print the last grade out a certain number
of times. Is this even possible or am i overthinking it?


No, you're not over-thinking it, but you do have a few small errors in 
your code:




Some code is:
def main():

gradesEntered = 0
score = 0
numberOfGrades = 0
#get the number of grades being checked
numberOfGrades = int(raw_input("Please enter the number of grades:" ))

#Begin a 'for' loop to enter each score
while numberOfGrades != gradesEntered:
grade = int(raw_input("Please enter the grade:" ))
gradesEntered += 1
score =+ grade
grade = [numberOfGrades]


Here you ask the user to enter a grade. Suppose they enter (say) 75. 
Your program will store 75 in grade, and then a moment later over-write 
that by storing [numberOfGrades] in grade.


I believe that what you need is to have a variable "grades" (note 
plural), and each time around the while loop, you need to append the 
current grade to the grades list.


Then, after you have collected all the grades, you can iterate over the 
list to get each grade one at a time:


for grade in grades:
print grade


See how you go with that, and don't hesitate to ask if anything is unclear!





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with this problem

2011-04-24 Thread Steven D'Aprano

Ratna Banjara wrote:

 The number 124 has the property that it is the smallest number whose
first three multiples contain the digit 2. Observe that
124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each
contain the digit 2. It is possible to generalize this property to be
the smallest number whose first n multiples each contain the digit 2.
Write a function named smallest(n) that returns the smallest number
whose first n multiples contain the digit 2. Hint: use modulo base 10
arithmetic to examine digits.

Its signature is
int smallest(int n)

You may assume that such a number is computable on a 32 bit machine,
i.e, you do not have to detect integer overflow in your answer.


Do you have a Python question, or would you just like us to do your 
homework for you?


This is a mailing list for learning Python. Please make an attempt to 
solve the problem, and if you still have trouble, show us what your code 
is, what error you get, and we'll try to help you with Python.




--
Steven



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Assistance

2011-04-24 Thread Krystal Brosz
Hi there,

i'm struggling with a program, i feel like i am really close to getting it
but i cannot find a way to use the target variables inside of a loop:
I'm trying to get the program to ask the user, how many grades are you going
to enter.  Then i want to work out the average which is fine.  But then i
want the loop to print out from each grade entered the letter grade as per
my if statement.  All it does is print the last grade out a certain number
of times. Is this even possible or am i overthinking it?

Some code is:
def main():

gradesEntered = 0
score = 0
numberOfGrades = 0
#get the number of grades being checked
numberOfGrades = int(raw_input("Please enter the number of grades:" ))

#Begin a 'for' loop to enter each score
while numberOfGrades != gradesEntered:
grade = int(raw_input("Please enter the grade:" ))
gradesEntered += 1
score =+ grade

grade = [numberOfGrades]
for i in range (numberOfGrades):
grade.append(i)
print determine_grade(grade,i)

#get the grade letter for scores
print "the total number of grades entered are:", score
print "The average of the scores is:", calc_average
print "The number of grades entered is:", gradesEntered


def determine_grade(grade,i):
if grade >= 90 and grade <= 100:
  result = 'Your grade is A'
elif grade >=80 and grade <= 89:
  result = 'Your grade is B'
elif grade >= 70 and grade <= 79:
  result = 'Your grade is C'
elif grade >=60 and grade <= 69:
  result = 'Your grade is D'
elif grade < 60:
  result = 'Your grade is F'
else:
print 'Error: Invalid grade.'
return result

main()

Thank you in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to solve this problem in python?

2011-04-24 Thread Ratna Banjara
 Write a function named countRepresentations that returns the number
of ways that an amount of money in rupees can be represented as rupee
notes. For this problem we only use  rupee notes in denominations of
1, 2, 5, 10 and 20 rupee notes.

The signature of the function is:
   def countRepresentations(int numRupees)

For example, countRepresentations(12) should return 15 because 12
rupees can be represented in the following 15 ways.
  1. 12 one rupee notes
  2. 1 two rupee note plus 10 one rupee notes
  3. 2 two rupee notes plus 8 one rupee notes
  4. 3 two rupee notes plus 6 one rupee notes
  5. 4 two rupee notes plus 4 one rupee notes
  6. 5 two rupee notes plus 2 one rupee notes
  7. 6 two rupee notes
  8. 1 five rupee note plus 7 one rupee notes
  9. 1 five rupee note, 1 two rupee note and 5 one rupee notes
  10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes
  11. 1 five rupee note, 3 two notes and 1 one rupee note
  12. 2 five rupee notes and 2 one rupee notes
  13. 2 five rupee notes and 1 two rupee note
  14. 1 ten rupee note and 2 one rupee notes
  15. 1 ten rupee note and 1 two rupee note

Hint: Use a nested loop that looks like this. Please fill in the
blanks intelligently, i.e. minimize the number of times that the if
statement is executed.
for (int rupee20=0; rupee20<=__; rupee20++)
   for (int rupee10=0; rupee10<=__; rupee10++)
  for (int rupee5=0; rupee5<=__; rupee5++)
 for (int rupee2=0; rupee2<=__; rupee2++)
for (int rupee1=0;  rupee1<=__; rupee1++)
{
if (___)
   count++
}

-- 
Regards,
Ratna P Banjara
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with this problem

2011-04-24 Thread Ratna Banjara
 The number 124 has the property that it is the smallest number whose
first three multiples contain the digit 2. Observe that
124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each
contain the digit 2. It is possible to generalize this property to be
the smallest number whose first n multiples each contain the digit 2.
Write a function named smallest(n) that returns the smallest number
whose first n multiples contain the digit 2. Hint: use modulo base 10
arithmetic to examine digits.

Its signature is
int smallest(int n)

You may assume that such a number is computable on a 32 bit machine,
i.e, you do not have to detect integer overflow in your answer.

-- 
Regards,
Ratna P Banjara
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] validating user input

2011-04-24 Thread ALAN GAULD
> "Rance Hall"  wrote

>
> >But you have just finished enumerating them and I holds
> >the index of the last item, or in your case the count of
> >the last item. But even without that you can always
> >use len() to find out how many menuchoices there are...
>
> I knew about len, but somehow didnt think of it in this context.
>
> So at the present time I have this:
>
> def buildmenu(menuchoices):
> for i, option in enumerate(menuchoices, 1):
>  print('%s. %s' % (i, option))
> validdata = False
> while not validdata:

instead of validData you could just use 

while True:

since you never change validData anyway and use a 
return to break out of the loop.

>menuchoice = int(input('\nYour Choice? '))
>if menuchoice >= 1 and menuchoice <= i:
> return menuchoice-1
>else:
> print("Entry not valid")

You can tidy up the if test slightly:

if ( 1 <= menuchoice <= i):
return menuchoice-1

Also why bother with the minus 1? 
You can use the values 1-n just as easily.


> Maybe the while not loop could have been avoided,
No, you need some kind of loop to repeat the choice.
Otherwise you just exit the program with an error, 
which seems a bit harsh!

> The issue I have now is that the input might not be an integer at all.
> If it isnt, then the int conversion that is part of the input line
> will die with a fatal error.

No, it will throw a ValueError which you can catch and 
handle exactly as you did above

while True
try:
menuchoice = 
if (...):
else
except ValueError:
except other errors


HTH,

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor