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

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Help with changing rules to if statements


    I am working on a program that based on the users input of X's and underscores prints out a pattern.
    Part 1
    -The string must contain at least 3 characters
    -The string must only contain X, x, or _in any order
    -The code then asks for the number of lines the pattern should be
    If the user breaks any of those rules they will be prompted with an error message.

    In the second part, for each line of output, you will determine how to replace each character of the previous line by looking at that character, its left neighbor, and its right neighbor.

    1.Left neighbor(in old line)
    2.Character(in old line)
    3.Right neighbor(in old line)
    4.Character(in new line)
    1 2 3 4
    X X X _
    X X _ X
    X _ X X
    X _ _ _
    _ X X X
    _ X _ X
    _ _ X X
    _ _ _ _

    -Consider the user's string to "wrap around" so that the last character is the first character's "left neighbor," and the first character is the last character's "right neighbor.
    Heres an example:
    using- X____X___X____ and 20 lines
    X____X___X____
    X___XX__XX___X
    X__XXX_XXX__XX
    X_XX_XXX_X_XX_
    XXXXXX_XXXXXXX
    _____XXX______
    ____XX_X______
    ___XXXXX______
    __XX___X______
    _XXX__XX______
    XX_X_XXX______
    XXXXXX_X_____X
    _____XXX____XX
    ____XX_X___XXX
    ___XXXXX__XX_X
    __XX___X_XXXXX
    _XXX__XXXX___X
    XX_X_XX__X__XX
    _XXXXXX_XX_XX_
    XX____XXXXXXX_

    The first part I have no problemThe problem is in part2. I have been able to do it but instead of using rules{} I need to use if statements. I have absolutely no clue to convert the rules into if statements or how that will change loop at the end. Please help with any suggestions that you can give. My whole code is below.
    Code:
    def main():
     
        while True:
     
            s1 = input('What is the initial string? ')
            s1 = s1.upper()
            count = 0
     
            if len(s1) < 3:
                print('Error: string must have at least 3 characters.')
                continue # restart loop
            else:
                for b in s1:
                    if b != "X" and b != "_":
                        count = count + 1
                    else:
                        if count > 0:
                            print('Error: string must contain only X and _.')
                
     
            # code for x and underscore
            #count = 0
            #for b in s1:
                #if b != "X" and b != "_":
                    #count = count + 1
     
            if count > 0:
                print('Error: string must contain only X and _.')
                continue # restart loop
     
            # passed all the checks, so exit the loop
            break
     
        while True:
            num_line = int(input('How many lines should I generate? '))
            if num_line < 0:
                print('Error: number of lines must be positive.')
            else:
                break
     
        print('Start string:',s1)
        print('Number of lines:',num_line)
     
        rules = {
            "XXX" : "_",
            "XX_" : "X",
            "X_X" : "X",
            "X__" : "_",
            "_XX" : "X",
            "_X_" : "X",
            "__X" : "X",
            "___" : "_"
        }
     
        print(s1)
     
        for row in range(num_line -1):
            # add wrap around padding
            s1 = s1[-1] + s1 + s1[0]
            s2 = ""
            for i in range(len(s1) - 2):
                # len(s1)-2 because the wrap around adds 2 to the length 
                nbrs = s1[i:i+3]
                s2 += rules[nbrs]
            s1 = s2
            print(s1)
    main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,928
    Rep Power
    481
    Code:
    def main():
    
        # shorter.  Use sets.################################################################
        while True:
            s1 = input('What is the initial string? ')
            s1 = s1.upper()
            if 2 < len(s1):
                break
            else:
                print('Error: string must have at least 3 characters.')
            if set('X_').issuperset(set(s1)):
                break
            else:
                print('Error: string must contain only X and _.')
    
        # longer, use try: except: What if the input doesn't match int? ################################################################
        while True:
            try:
                num_line = int(input('How many lines should I generate? '))
            except:
                print('Error: please enter an integer.')
            else:
                if num_line < 0:
                    print('Error: number of lines must be positive.')
                else:
                    break
    
        print('Start string:',s1)
        print('Number of lines:',num_line)
    
        rules = {
            "XXX" : "_",  # 0
            "XX_" : "X",  # 1
            "X_X" : "X",  # 2
            "X__" : "_",  # 3
            "_XX" : "X",  # 4
            "_X_" : "X",  # 5
            "__X" : "X",  # 6
            "___" : "_",  # 7
        }
    
        print(s1)
    
        for row in range(num_line):
            # add wrap around padding
            s1 = s1[-1] + s1 + s1[0]
            s2 = ""
            for i in range(len(s1) - 2): # len(s1)-2 because the wrap around adds 2 to the length
                nbrs = s1[i:i+3]
                # s2 += rules[nbrs]  # this is good code I'm sorry you had to lose it.
    
                if nbrs in 'xxx x__ ___'.upper().split(): # rules 0 3 7
                    s2 += '_'
                else:
                    s2 += 'X'
    
            s1 = s2
            print(s1)
    
    main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo