Just started python...

Hello everyone I just started python last night and I was fallowing this wonderful tutorial:
http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python
I am going to use this thread to ask for questions and get help for when I run into road blocks. So… here goes nothing!
Questions:

  1. Is there a way to limit the amount of characters a user is supposed to input using a line like:
input = raw_input("Enter 5 to 20 characters: ")

?

  1. Is there a way to make it so the * symbol appears when typing, like a password?
  2. When using a line such as:
phonenumber = input("Enter your phone number: ")

Can you make it so ONLY numbers will appear when you press them? If you enter in letters, it will give you an error after you press enter.

This is my progress so far, I have just been experimenting with things I’ve learned from the tutorial. I just kinda compiled them into a simple script. This is the menu part of it, I will now add a dictionary with accounts and passwords.

 
print "Login script test."
print
print
print
def bye():
 print "Bye!"
 input = "q"
def confirm():
 yes =  raw_input("Enter y to continue: ")
 print "----------"
 
 if yes == "y":
  menu()
 else:
  confirm()
 
 
def menu():
 print
 print "---Menu---"
 print "c = to create an account."
 print "l = to login to an account."
 print "q = to quit."
 print
 
input = "x"
menu()
while input != "q":
 input = raw_input("Enter new command: ")
 print
 print
 print
 if input == "c":
  print "This creates a new account..." 
  confirm()
 
 elif input == "l":
  print "This will login to an account."
  confirm()
 
 elif input == "q":
  bye()
 
 else:
  print "Command not recognized, please enter a new one."
  confirm()
 

Cool. Are you using the BGE?
If not:

  1. No. You can always get the number of characters of a string, (len()), but you can’t get the string while you’re typing, as far as I know.
  2. Not as far as I know.
  3. You can make a loop that breaks only when the user doesn’t type characters, like this:

while 1:
    try:
        phonenumber = input("Enter your phone number: ")
        break
    except:
        print "You entered characterz. Pleez try again witout typin charaterz, unly numb3rz."

With BGE, the answer to all that is yes.
And to print many spaces, you can do something like this:
print "
"*number_of_spaces

For the first one, count the characters after they are input. If there are too few or too many, print an advisory message, throw out the input and loop back to get new input.

I think you might be better off asking for one character at a time, then parsing it – is it a number? is it a character? Are there enough? Is this the last one? Echo the typing with an asterisk. Get the next character.

You’ll need to know how to supress any automatic echoing of a character when it’s typed so you can control whether you display an asterisk when the user enters a number or a bleep noise when the user makes a mistake.

Pseudocode parser

for loop = 1 to maximum_chars
       get a character
       increment loop
       if number 
            then print "*" 
       if RETURN and loop>minimum_chars-1 
           then end loop
       else print bleep, decrement loop 
loop

I have no idea what that would look like in Python.

Don’t use “input”, people can pass arbitrary pieces of code in!

Take a raw input and parse it, or use exceptions:


finished = False

while not(finished):
  try:
     woo=raw_input("ENTER THE NUMBER:")
     yay=int(woo) #cast it to an integer
     finished=True
  except ValueError: #this means cast failed
    print "No, no, no, that wasn't a number"

Not tested, but that should work.

Edit - passwords: [grumble grumble found on google as first result with “python enter password stars”]


import getpass
password = getpass.getpass("Enter password:")

Ok, so this is how I tried to add it the first piece of code that Ian C gave me:

 
import getpass
 
def print_menu():
    print '1. Display current accounts'
    print '2. Create an account'
    print '3. Delete an account'
    print '4. Lookup a password'
    print '5. Quit'
    print
 
menu_choice = 0
accounts = {}
print_menu()
while menu_choice != 5:
 finished = False
 while not(finished):
   try:
      menu_choice = raw_input("Choose a number (1-5):")
      yay=int(menu_choice) #cast it to an integer
      finished=True
   except ValueError: #this means cast failed
     print "That wasn't a number, please re-try."
 
     if menu_choice == 1:
         print "Accounts:"
         for x in accounts.keys():
             print "Account Name: ", x, "	Password:", accounts[x]
         print
     elif menu_choice == 2:
         print "Create account"
         name = raw_input("Account Name: ")
         password = getpass.getpass("Enter password:")
         accounts[name] = password
     elif menu_choice == 3:
         print "Remove Account"
         name = raw_input("Account Name: ")
         if accounts.has_key(name):
             del accounts[name]
         else:
             print name, "Does not exist."
     elif menu_choice == 4:
         print "Lookup a password"
         name = raw_input("Account Name: ")
         if accounts.has_key(name):
             print "The password is", accounts[name]
         else:
             print name, "Does not exist."
     elif menu_choice != 5:
         print_menu()

The password thingy works great, except * don’t appear, it’s just blank, which is fine as well. Gee, I never though of Google, that makes me feel really dumb .:spin:

thanks for all the replies and the help,
-Rozzy

Edit: Fixed it!

import getpass
 
def print_menu():
    print '1. Display current accounts'
    print '2. Create an account'
    print '3. Delete an account'
    print '4. Lookup a password'
    print '5. Quit'
    print
 
menu_choice = 0
accounts = {}
print_menu()
while menu_choice != 5:
    finished = False
    while not(finished):
      try:
         yay = raw_input("Choose a number (1-5):")
         menu_choice = int(yay) #cast it to an integer
         finished=True
      except ValueError: #this means cast failed
         print "That wasn't a number, please re-try."
    if menu_choice == 1:
        print "Accounts:"
        for x in accounts.keys():
            print "Account Name: ", x, "	Password:", accounts[x]
        print
    elif menu_choice == 2:
        print "Create account"
        name = raw_input("Account Name: ")
        password = getpass.getpass("Enter password:")
        accounts[name] = password
    elif menu_choice == 3:
        print "Remove Account"
        name = raw_input("Account Name: ")
        if accounts.has_key(name):
            del accounts[name]
        else:
            print name, "Does not exist."
    elif menu_choice == 4:
        print "Lookup a password"
        name = raw_input("Account Name: ")
        if accounts.has_key(name):
            print "The password is", accounts[name]
        else:
            print name, "Does not exist."
    elif menu_choice != 5:
        print_menu()