2

I'm novice in python and got a problem in which I would appreciate some help. The problem in short:

  1. ask for a string
  2. check if all letter in a predefined list
  3. if any letter is not in the list then ask for a new string, otherwise go to next step
  4. ask for a second string
  5. check again whether the second string's letters in the list
  6. if any letter is not in the list then start over with asking for a new FIRST string

basicly my main question is how to go back to a previous part of my program, and it would also help if someone would write me the base of this code. It start like this:

list1=[a,b,c,d]
string1=raw_input("first:")
for i in string1:
    if i not in list1:

Thanks

1
  • BTW: It should be list1=['a','b','c','d'] in your code. Commented Feb 25, 2011 at 15:05

4 Answers 4

3

I suggest you start here: http://docs.python.org/tutorial/introduction.html#first-steps-towards-programming

And continue to next chapter: http://docs.python.org/tutorial/controlflow.html

Sign up to request clarification or add additional context in comments.

Comments

0

You have a couple of options, you could use iteration, or recursion. For this kind of problem I would go with iteration. If you don't know what iteration and recursion are, and how they work in Python then you should use the links Kugel suggested.

1 Comment

N.B. This was my first time answering a question on SO, so if anyone has any comments on my answer, please let me know :)
0

This sounds like a job for a while loop http://www.tutorialspoint.com/python/python_while_loop.htm

pseudo code is

list=[a,b,c,d]
declare boolean passes = false

while (!passes)
    passes = true
    String1 = raw_input("first:")
    foreach char in string1
       if !list.contains(char)
            passes = false
            break

    if passes
        String2 = raw_input("second:")
        foreach char in string2
           if !list.contains(char)
                passes = false
                break

Comments

0

Another good place to start is by looking for common sequences of action and putting them in a separate subroutine.

# ignore this bit - it's here for compatibility
try:
    inp = raw_input  # Python 2.x
except NameError:
    inp = input      # Python 3.x

# this wraps the 'ask for a string, check if all characters are valid' bit in a single call
def GetValidString(msg, validChars):
    i = inp(msg)
    if all(ch in validChars for ch in i):
        return i
    else:
        return None

def main():
    while True:
        str1 = GetValidInput('first: ', 'aeiou'):
        if str1:
            str2 = GetValidInput('second: ', 'rstvy'):
            if str2:
                break  # good! we can leave the loop now

    # got valid values for str1 and str2

the logic looks like 'loop until you get string1 and it is good and you get string2 and it is also good'.

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.