[Tutor] While Loop?

Danny Yoo dyoo at hashcollision.org
Wed May 14 18:16:10 CEST 2014


On Wed, May 14, 2014 at 2:45 AM, Sam Ball <samball72 at hotmail.com> 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.
>
> So far I have...
>
>
>
> userAccountNumber = eval(input("Please Input Your Account Number:"))


Don't use eval here.  If you want to turn a string that's full of
digits into a number, use int()

    https://docs.python.org/3/library/functions.html#int

eval() is dangerous.  Don't use it.



> while userAccountNumber > 100000000 or <=9999999:
>     print userAccountNumber ("Invalid Account Number! Account Must Be Eight
> Digits")
>
> When I run this however it gives me an invalid syntax and highlights the =
> in <=9999999:
>
> Would appreciate any help with sorting this out.



The compiler is technically correct: it's invalid syntax.

You've written something that scans as English.  But it does not scan
as a Python program.  "or" does not have the English meaning in
Python.

To put it another way: what are you comparing to be less than or equal
to 99999999?  You've left that implicit.  Many programming languages
do not allow implicit statements because it's too easy to
misunderstand or misinterpret.

Make what you're comparing explicit.


More information about the Tutor mailing list