#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Beginner question! Simple!


    Alright so I am trying to learn programming since I find it very exciting. But I have problem finding any information about a problem I have encountered. I am simply trying to write a "Hello World" program and am trying to limit the input to only letters.

    This is my code sofar.

    print ('Hello world!')
    print ('What is your name?')
    myName = input ()
    if myName = str
    print ('Nice to meet you, ' + myName)
    else
    print ('Try again')

    Help anyone?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    76
    Rep Power
    2
    My version:

    Code:
    def fbValidCharacters(sString):
    	"Returns True if sString only consists of valid characters"
    	sValidCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	for s in sString:
    		if s not in sValidCharacters:
    			return False
    	return True
    
    print ('Hello world!')
    while True:
    	sMyName = input('What is your name?')
    	if not fbValidCharacters(sMyName):
    		print("you can not use any special characters in your name",
    			"try again")
    		continue
    	print ('Nice to meet you, ' + sMyName)
    	break
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by leonnaley2
    My version:

    Code:
    def fbValidCharacters(sString):
    	"Returns True if sString only consists of valid characters"
    	sValidCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	for s in sString:
    		if s not in sValidCharacters:
    			return False
    	return True
    
    print ('Hello world!')
    while True:
    	sMyName = input('What is your name?')
    	if not fbValidCharacters(sMyName):
    		print("you can not use any special characters in your name",
    			"try again")
    		continue
    	print ('Nice to meet you, ' + sMyName)
    	break
    How come you are calling the valid characters as "fbValid..." and "sValid..."?

    I changed the code to (and it still works):

    def ValidCharacters(String):
    "Returns True if sString only consists of valid characters"
    ValidCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for s in String:
    if s not in ValidCharacters:
    return False
    return True

    print ('Hello world!')
    while True:
    MyName = input('What is your name?')
    if not ValidCharacters(MyName):
    print("you can not use any special characters in your name",
    "try again")
    continue
    print ('Nice to meet you, ' + MyName)
    break

    Is it easier to keep track of the variables with your method?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Mathematically the character test is a set operation. Use sets. The program is clearer and faster.
    Code:
    # For doctests command
    # python -m doctest thisfile.py
    
    def fbValidCharacters(sString):
        '''
            Returns True if sString only consists of valid characters
            >>> fbValidCharacters('My Name')
            True
            >>> fbValidCharacters('Dr. Zhivago')
            False
        '''
        sValidCharacters = set(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
        return sValidCharacters.issuperset(set(sString))
    
        # Operator notation available if you prefer
        return set(sString) <= sValidCharacters


    Also disentangling of the program into separate get and use blocks is most often better design.
    Code:
    # acquire the data
    while True:
        sMyName = input('What is your name?')
        if not fbValidCharacters(sMyName):
            break
        print("you can not use any special characters in your name",
              "try again")
    
    # use the data
    print('Nice to meet you, ' + sMyName)
    Last edited by b49P23TIvg; November 11th, 2013 at 01:16 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo