[Tutor] While Loop?

Dave Angel d at davea.name
Wed May 14 22:16:45 CEST 2014


On 05/14/2014 05:45 AM, Sam Ball wrote:
> I'm attempting to create a program where the user inputs their account number (which must be 8 digits) and if what the user enters is not 8 digits in length I want python to tell the user this is invalid and then keep asking for the account number until a suitable number has been entered.
>   I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new to Python.
>

Welcome to Python, and to the Tutor mailing list.  Please post in text 
form, as the html you're now using is quite error prone, especially 
since this is a text list.  There's no errors I can see with your 
present message, but it's a good habit to learn early - tell your email 
program to use "plain text".

Next, it's very important to specify what version of Python you're 
using.  In particular there are differences between Python version 2.x 
and version 3.x   And those differences affect the input statement.

 From your print syntax, it appears you're aiming at Python 2.x  But 
it's not valid there either, so it's better if you be explicit in your 
question.


> So far I have...
>
>
>
>
> userAccountNumber = eval(input("Please Input Your Account Number:"))

Since I'm asssuming Python 2.x, you want   int(raw_input(   instead. 
input() in 2.x is a security mistake, and eval() is for either version 
of Python.


> while userAccountNumber > 100000000 or <=9999999:

Syntax error, as you've discovered.  Alan has told you how to fix it. 
But it would be much better to get in the habit of quoting the entire 
error message (it's called a traceback) instead of paraphrasing it.

>      print userAccountNumber ("Invalid Account Number! Account Must Be Eight Digits")

Once you've fixed the above, you'll get an error on this line.  But how 
to fix it depends on what Python version you're using. 
userAccountNumber isn't a function, so why are you trying to call it?

Next problem is that your loop doesn't ask the user again, it just loops 
around trying to print its error message.
>
>
>
> When I run this however it gives me an invalid syntax and highlights the = in <=9999999:
>
> Would appreciate any help with sorting this out.
>

Once you've gone as far as you can get with these suggestions from Alan, 
Danny, and me, be sure and use reply-list to post your next revision and 
query.  (Or, if your email program doesn't support that, use Reply-All, 
and remove from the header everyone you don't want to get the response). 
  And of course, you'll tell us what version of Python you're 
taqrgeting.  In particular, keep the tutor at python.org recipient.

Once your program works, you need to consider other things, possible 
bugs.  For example, what happends if a user types  "charlie" instead of 
a number?  Or if they type a floating point value?  What would you like 
to happen?

-- 
DaveA


More information about the Tutor mailing list