Python: Unable to terminate loop with different type of variable -


i have made program stores login details have entered , allows login main part of menu. working fine when data types strings, example; modea = input("etc") allow user select options entering number inputs correspond option, e.g "2", should call assigned function fulfills if modea == 2 statement, if try change string integer data type, example modea = int(input("etc")), if enter expected input, loop never stop , continue running if entered nothing, contrary using modea = input("etc") treats string, , still accept number input , carry out assigned action, not if data type integer?

i added full code here make more sense, can ignored runs intended, can't seem head around whats going on logged()function part of code attempted modea = int(input("etc")) work terminating loop when if statements fulfilled, continues on looping if null. appreciated.

#import modules import time import sys import re  #initialise variables error handling name = "" mode = "" username = "" password = "" addusername = "" addpassword = "" exit = "" modea = "" new_app = "" new_pass = "" quit = ""  #dictionary store more 1 set of login details vault = {}  #lists store website names , passwords appvault = [] passvault = []   #first menu , registering login details -----------------------------------------------------------------------------------------------------------------------------------------  def menu(): #the menu function 1st menu asks user either register login details or login existing login details saved vault dictionary     print("--------------------------*entering main menu*-----------------------------------\n") #triple """ used format menu     mode = input(str("""hello {}, below modes can choose from:\n      ##########################################################################     a) login username , password     b) register new user     select mode, enter corresponding letter of mode below     ##########################################################################\n     > """.format(name))).strip() #greets user , removes white space     return mode #returns input  def login(username, password): #the login function user logins in existing registered details in vault dictionary     if len(vault) > 0 : #user has append usernames , passwords before asks login details         print("welcome {} login console".format(name))         noloop = true #starts loop if true begin looping         while noloop:             addusername = input("please enter username: ")              if addusername == "":                 print("username not entered, try again!")                 continue #returns beginning of while loop if null             addpassword = input("please enter password: ")              if addpassword == "":                 print("password not entered, try again!")                 continue #returns beginning of while loop if null             try:                 if vault[addusername] == addpassword: #if username , password match login details appended dictionary, count matching details.                     print("username matches!")                     print("password matches!")                     print("logging password vault...\n")                     noloop = logged() #jumps logged function , tells user logged on                     return noloop #to return noloop depending on users option (true or false)             except keyerror: #the except keyerror recognises existence of username , password in list                 print("the entered username or password not found!")      else:         print("you have no usernames , passwords stored!") #when user selects option before adding login details dictionary         return true #returning start of loop when else statement printed  def register(): #example username appended. same applies password     global username #initialises global variables     global password     print("please create username , password password vault.\n")      while true:         validname = true #whileloop loop username variable , while true runs loop         while validname:             username = input("please enter username add password vault. note: username must @ least 3 characters long: ").strip().lower()             if not username.isalnum(): #handles usernames no input, contain spaces between them or have symbols in them.                 print("your username cannot null, contain spaces or contain symbols \n")             elif len(username) < 3: #any username less 3 characters rejected , looped                 print("your username must @ least 3 characters long \n")             elif len(username) > 30: #any username more 30 characters rejected , looped                 print("your username cannot on 30 characters long \n")             else:                 validname = false #whileloop false mean loop break , proceed onto asking password         validpass = true #whileloop loop password variable , while true runs loop          while validpass:             password = input("please enter password add password vault. note: password must @ least 8 characters long: ").strip().lower()             if not password.isalnum(): #handles null input passwords, passwords contain spaces or symbols                 print("your password cannot null, contain spaces or contain symbols \n")             elif len(password) < 8: #passwords less 8 characters long rejected , loops                 print("your password must @ least 8 characters long \n")             elif len(password) > 20: #passwords more 20 characters long rejected , loops                 print("your password cannot on 20 characters long \n")             else:                 validpass = false #the validpass has true stay in function, otherwise if false, execute action, in case password appended.         vault[username] = password #the appending process of username , passwords dictionary         validinput = true #whileloop asking user if want quit or add more login details         while validinput:             exit = input("\ndo want register more usernames , passwords? enter 'end' exit or key continue add more username , passwords:\n> ")             if exit in ["end", "end", "end"]: #if input matches these conditions, loop break , jump main menu                 return #returns outputs called function             else:                 validinput = false #if user decides not quit , add more login details, register called , process of asking login input repeats                 register()         return register #returns login details function   #logged onto password , website app adding console------------------------------------------------------------------------------------------------------------------------  def logged(): #the logged function 2nd menu user able access after logging in registered login details     print("-----------------------------*entering password vault menu*-----------------------------------\n")     print("you logged in, welcome password vault console.")     keeplooping = true #whileloop true keep menu in loop     while keeplooping:             try:             modea = int(input("""below options can choose in password vault console:             ##########################################################################\n             1) find password existing website/app             2) add new website/app , new password             3) summary of password vault             4) exit             ##########################################################################\n             > """))         except valueerror:             print("please enter valid option!")      if modea == 1: #if user enters 1 instructed, have decided choose find existing apps , passwords         viewapp() #viewapp function called      elif modea == 2: #if user enters 2 instructed, have decided add new app/websites , passwords         addapp() #addapp function called      elif modea == 3: #if user enters 3 instructed, have decided @ summary of passwords entered far         summary() #summary function called      elif modea == 4: #if user enters 4 instructed, have deicided quit entire program. loops stop , program ends print statement saying "goodbye"         keeplooping = false          print("*exiting program*\n")         time.sleep(2)         print("############################################################")         print("goodbye user , using password vault program")         print("############################################################")         return keeplooping      else:         print("that not valid option, please try again: ")          validintro = false    def viewapp(): #the viewapp function 1st option of password , vault menu allows user view app/websites , passwords stored far     if len(appvault) > 0: #the website/apps , passwords shown once user has entered set of info, otherwise no info shown         print("""below details of website/apps , passwords stored far:  (note: websites/apps , passwords shown in order of being appended; 1st password 1st website, etc...\n""")         app in appvault:              print("here website/app have stored:")             print("- {}\n".format(app)) #website/app printed out here     if len(passvault) > 0 : #the website/apps , passwords shown once user has entered set of info, otherwise no info shown         code in passvault:              print("here password have stored websites/apps: ")             print("- {}\n".format(code)) #the passwords printed out here      else:         print("you have no apps or passwords entered yet!")   def addapp():      while true:         validapp = true          while validapp:             new_app = input("enter new website/app name: ").strip().lower()             if len(new_app) > 20: #if user enters website more 20 characters, rejected , loops , asks input again                 print("please enter new website/app name no more 20 characters: ")             elif len(new_app) < 1: #if user enters nothing, program loops , asks input again                 print("please enter valid new website/app name: ")             else:                 validapp = false                  appvault.append(new_app)          validnewpass = true          while validnewpass:             new_pass = input("enter new password stored in passsword vault: ").strip()             if not new_pass.isalnum(): #checks if entered username has spaces, or symbols or null input, rejected , program loop                 print("your password website/app cannot null, contain spaces or contain symbols \n")                         elif len(new_pass) < 8: #the password must @ least 8 characters long accepted valid                 print("your new password must @ least 8 characters long: ")             elif len(new_pass) > 20: #the password must not exceed 20 characters, otherwise rejected , loop                 print("your new password cannot on 20 characters long: ")                else:                 validnewpass = false                  passvault.append(new_pass)           validquit = true          while validquit:             quit = input("\ndo want enter more websites/apps , passwords? enter 'end' exit or key continue add more website/app names , passwords them: \n> ")             if quit in ["end", "end", "end"]:                  return             else:                 validquit = false                  addapp()              return addapp          def summary(): #the summary function print statement of how many password have been stored, passwords longest or shortest characters displayed     if len(passvault) > 0: #the user must have entered @ least 1 password before summary option can viewed             print("----------------------------------------------------------------------")             print("here summary of passwords stored in password vault:\n")             print("the number of passwords stored far:", len(passvault)) #len counts amount of passwords stored in passvault list             print("passwords longest characters: ", max(new_pass (new_pass) in passvault)) #max finds passwords appended in passvault list longest password             print("passwords shortest charactrs: ", min(new_pass (new_pass) in passvault)) #min finds passwords appended in passvault list shortet password             print("----------------------------------------------------------------------")     else:         print("you have no passwords entered yet!")       #main routine print("welcome user password vault program") print("in program able store usernames , passwords in password vaults , view them later on.\n") validintro = false  while not validintro:     name = input(str("greetings user, name?: ")).lower().strip()     if not re.match("^[a-z]*$", name):         print("your name must contain letters a-z: ")     elif len(name) < 1:          print("please enter name valid: ")     elif len(name) > 30:         print("please enter name no more 30 characters long: ")     else:         validintro = true          print("welcome password vault program {}.\n".format(name))  #the main program run in while loop program keep on going menu part of program more input till user wants program stop validintro = false  while not validintro:          chosen_option = menu()          validintro = false          if chosen_option in ["a", "a"]:              validintro = not login(username, password)          elif chosen_option in ["b", "b"]:              register()           else:             print("""that not valid option, please try again:\n """)              validintro = false  

you have indentation error.

your while keeplooping: loop in logged() contains try / except block keeps looping on , over. think want indent if statements below it.

edit based on comment:

when try/except reaches except prints statement , keeps on going. haven't included in except statement tell while loop start again. since assignment of modea failed, isn't assigned , results in error when try reference it. if want try input when user puts in bad value can use continue:

except valueerror:         print("please enter valid option!")         continue 

this cause whole instruction string print again — there may better logic, @ least prevent error.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -