| Store | Cart

Re: [Tutor] help with code

From: Alan Gauld via Tutor <tut...@python.org>
Sun, 11 Mar 2018 11:41:09 +0000
On 11/03/18 04:20, Leslie SimondeMontfort via Tutor wrote:
> Hi, I wondered if there is someone that can help me with this code. 

I'll try but theres a lot to comment on.
See below...

> def print_menu():       ## Your menu design here>     print 30 * "-" , "MENU" , 30 * "-">     print "1. Menu login 1">     print "2. Menu create an account 2">     print "3. Menu list all accounts 3">     print "4. Quit">     print 67 * "-">     >     loop!=4

That last line makes no sense. Its a boolean expression using an
undeclared variable(loop) You should get a name error when you run
it.

> while loop:          ## While loop which will keep going until loop = False

Again you reference loop but you have not assigned any
value to it. You should get an error here too.

>     print_menu()    ## Displays menu>     choice = input("Enter your choice [1-4]: ")

This is the end of your while loop and you have not modified
the test condition (loop) so even if loop existed this would
just loop forever. One of the fundamental principles of while
loops is that you must either modify the test condition
somehow or use a break somewhere inside the loop.

Also using input to convert your data is a bad design. ijput is very
insecure and users could (deliberately or by accident) type values which
python then executes and cause damagew, in extreme cases even crashing
your computer. Its much better to explicitly convert the raw_input string:

     choice = int(raw_input("Enter your choice [1-4]: "))

To catch invalid input wrap it in a try/except block...


> if choice==1:"Menu login 1"> 	count = 0

This doesn't work either. You have an if test where the
body is the single line containing the string. It doesn't
do anything  although it is legal Python but...
The next line is indented as if it were part of the if
block but it isn't so you should get an error there too.

> while True: >   def User():>     login = raw_input("Enter login name: ")>     passw = raw_input("Enter password: ")> >     # check if user exists and login matches password>     if login in users and users[login] == passw: >         print "\nLogin successful!\n">     else:>         print "\nUser doesn't exist or wrong password!\n">         if choice == 3:>         print("Too many attempts.")>         exit()

Now you have a loop that will go forever continually defining
(but not calling) a function.

Inside the function you might want to check the getpass
function from the getpass module for a more secure password
input. If you are on Unix/Linux/MacOS you could also look
at the pwd module for ways of validating the user.

Fiunally note that the if coice === 3 line is inside
the invalid user block so would only be executed if it
was an invalid user - which seems wrong?

In fact I'd suggest that if block should not even be
inside the User() function at all. Its not really checking
anything about the user, its checking what the user did.
Try to keep your functions purpose clear and distinct,
don't mix functionality in a single function.

> for val in "string":>     if val == "i":>         break

This assigns val to s,then t then r then i so it will allways break on
the 4th cycle.

You might as well just write

for val in 'str':

>     print(val)

And since you just print val you might as well miss out the loop and use

print('s\nt\nr\n')

> print("The end")>        >    if choice==2:     >        print " Menu create an account 2"

This is indented but there is no enclosing block so
should give an indentation error.

> def main():>     register()> > def register():>     username = input("Please input your desired username ")>     password = input("Please input your desired password ")>      if not username or not password:>             print "Enter a valid username and password...">         users = c.get_all_users()>         userList = users['list_users_response']['list_users_result']['users']

Note the indentation is all over the place.
Python is fussy about consistent indentation, you will get errors here.

Also you reference c but c does not appear anywhere else.
What is c?

The remaining code has more of the same so I'll stop here.
I think you need to strip this back and get the basic structure working
first before trying to add all the other functions.

print a menu, read the input and print the users choice.
Exit when option 4 is used.

Get that working then you can add the code to process
the choices one by one. Get each choice working before
adding the next.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tut...@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Recent Messages in this Thread
Leslie SimondeMontfort via Tutor Mar 11, 2018 04:20 am
Joel Goldstick Mar 11, 2018 11:16 am
Cameron Simpson Mar 11, 2018 11:39 am
Alan Gauld via Tutor Mar 11, 2018 11:41 am
Bob Gailer Mar 11, 2018 03:17 pm
Abdur-Rahmaan Janhangeer Mar 11, 2018 03:41 pm
Messages in this thread