On 10Mar2018 23:20, Leslie SimondeMontfort <mi...@yahoo.com> wrote:
Hi, I wondered if there is someone that can help me with this code.  Thank you, 

Generally we like to know what you think is wrong with it. For example, what you expected it to do, and a little transcript of what it actually did (cut/paste the text from your terminal/console straight in this email, complete with any stack traces that came with it, if there were any).

This looks like homework, and we're generally happy to help if you'll explain what you're trying to achieve and show the code you have so far. I'm glad to see you've included all your code here, it is usually necessary.

Absent an actual error transcript I can make a few general remarks about the code which might lead you towards making it work the way you intend it to.

Remarks inline below, in the code:

## Text menu in Python

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 * "-"


The line above "loop!=4" is a simple expression. It is valid, but useless. Normally a test with be part of a statement, such as the condition in an "if" or "while".

Also, because it is indented it is inside the "print_menu" function,
which doesn't seem very relevant - the function shouldn't care about
the value of "loop".

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

You haven't set a value to "loop" yet, so this code will raise an exception as soon as you try to test "loop". Probably it should be set to True before the loop begins.

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

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

Indentation is important in Python. Because this "if" statement is not indented, it is outside the "while" loop above. It looks like you while loop is meant to offer menu choices until the user chooses "Quit". So all the code acting on those choices needs to be inside the loop, and thus indents the same as the 2 lines above it which print the menu and read the choice.

You're setting "count = 0" but I don't see you use "count" elsewhere.

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"
       print "\nUser doesn't exist or wrong password!\n"
       if choice == 3:
       print("Too many attempts.")

This probably doesn't do what you imagine. A "def" statement is a statement, to be run. This loop runs forever, but all it does in the loop is _define_ the function "User". It never calls the function. So it will just run forever, redefining the function again and again.

Probably you need to define the function _before_ the loop, and actually call the function inside the loop.

Normally all your function definitions will be outside the main code, so the "User" function definition should be above this, for example right underneath your "print_menu" function definition. Likewise your other functions.

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

I presume this is just a little exercise loop to print some of the leading characters from the string "string".

print("The end")

  if choice==2:
      print " Menu create an account 2"

This "if" statement _is_ indented correctly for the main loop you set up earlier, but all the intervening code will be getting in the way. Once you have all the function definitions up the top of your code things will work better.

def main():

Note that this just defines a "main" function. Nothing seems to call it.

def register():
   username = input("Please input your desired username ")
   password = input("Please input your desired password ")
    if not username or not password:

This "if" is indented differently to the other statements in this function. It needs to move left one space.

You seem to be using Python 2. In Python 2, input() actually evaluates the input string as a Python expression. This is why you got an integer earlier when you prompted the user for "choice". However, when reading strings such as usernames or passwords you need to use raw_input(), not input(). raw_input() just reads the string and returns it.

           print "Enter a valid username and password..."

Here's you are admonishing the user for entering an empty username or password. However you don't then do anything about it; instead your code falls through and starts setting up the user.

You probably want a loop shapes more like this (totally untested):

   while True:
       username = input("Please input your desired username ")
       if len(username) == 0:
           print("Empty username rejected.")
       password = input("Please input your desired password ")
       if len(password) == 0:
           print("Empty password rejected.")

which will keep prompting until both the username and password are acceptable. _Then_ it breaks out of the loop and proceeds.

       users = c.get_all_users()
       userList = users['list_users_response']['list_users_result']['users']
       if username in userList:
           print "username already exist"

This "else" needs to be indented the same as the "if".

           # I just want to create if fields are not empty and if username dont 
   file = open("credential.txt","a")
   file.write(" ")

It seems odd to call the "login" function after creating the user.

if choice==3:

Again, this "if" should be indented for the main loop like the other "choice" "if" statements.

def login():
   check = open("credential.txt","r")

Here you're opening the credential.txt file, but not doing anything with it.

   username = input("Please enter your username")
   password = input("Please enter your password")

Again, these lines should call raw_input(), not input().

def thank_you():
 print("Thank you for signing up at our website!.")

You define this function but never call it.

   elif choice == "q":

The call to exit() needs to be indented in order to be inside the "elif".

Also, exit() is not a builtin function. I need to import it. Normally you would put this import at the very top of your program, such as:

 from sys import exit

I also notice here that you're expecting the user to be able to enter a "q" instead of a "4". So this doesn't match your menu. But if you do want the user to enter a "q" the you need to use raw_input() instead of input() in your menu prompt, and your other "choice" values need to be strings, such as "1", "2" and so forth.

Cameron Simpson <c...@cskk.id.au>
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

Reply via email to