Skip to content
Advertisement

My code outputs everything right but it doesnt exit after finding out an error

question sample input outputs to check, an input is 221, 1, 7 the output shud be invalid move as the 7th position of 0000221 is 1 already not 0 so it shud be an invalid move which my validation checker does correct except after that it still prints out the whole thing because there is no error with printing it out but i dont want it to print after finding the invalid move.

long_number = int(input()) #represents the 7-digit number to explore
player_number = int(input()) #the number the player inputs
column_number = int(input()) #represents the column of the 7-digit number to explore

if column_number < 1 or column_number > 7:
    print("invalid move")
elif player_number == 1 or player_number == 2:
    r_num1 = long_number % 1000000
    digit_pos1 = long_number // 1000000

    r_num2 = r_num1 % 100000
    digit_pos2 = r_num1 // 100000

    r_num3 = r_num2 % 10000
    digit_pos3 = r_num2 // 10000

    r_num4 = r_num3 % 1000
    digit_pos4 = r_num3 // 1000

    r_num5 = r_num4 % 100
    digit_pos5 = r_num4 // 100

    r_num6 = r_num5 % 10
    digit_pos6 = r_num5 // 10

    r_num7 = r_num6 % 1
    digit_pos7 = r_num6 // 1

if column_number == 1:
    if digit_pos1 == 0:
        digit_pos1 = player_number
elif column_number == 2:
    if digit_pos2 == 0:
        digit_pos2 = player_number 
elif column_number == 3:
    if digit_pos3 == 0:
        digit_pos3 = player_number    
elif column_number == 4:
    if digit_pos4 == 0:
        digit_pos4 = player_number
elif column_number == 5:
    if digit_pos5 == 0:
        digit_pos5 = player_number
elif column_number == 6:
    if digit_pos6 == 0:
        digit_pos6 = player_number
elif digit_pos7 == 0:
        digit_pos7 = player_number
else:
    print("invalid move")

def digit_pos_check               
    if digit_pos1 > 0:
        print(digit_pos1,digit_pos2,digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos2 > 0:
        print(digit_pos2,digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos3 > 0:
        print(digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos4 > 0:
        print(digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos5 > 0:
        print(digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos6 > 0:
        print(digit_pos6,digit_pos7, sep='')
    elif digit_pos7 > 0:
        print(digit_pos7)
    else:
        print("invalid move")
        return digit_pos_check

Advertisement

Answer

Sounds like you want to stop your programme execution after reaching an invalid move. Wrap the programme in a function, and return after the invalid print statement, like:

print('invalid move')
return

this would allow the program to skip the rest of the functions and therefore no longer print anything else.

try this:

def main():
    long_number = int(input()) #represents the 7-digit number to explore
    player_number = int(input()) #the number the player inputs
    column_number = int(input()) #represents the column of the 7-digit number to explore

    if column_number < 1 or column_number > 7:
        print("invalid move")
        return
    elif player_number == 1 or player_number == 2:
        r_num1 = long_number % 1000000
        digit_pos1 = long_number // 1000000

        r_num2 = r_num1 % 100000
        digit_pos2 = r_num1 // 100000

        r_num3 = r_num2 % 10000
        digit_pos3 = r_num2 // 10000

        r_num4 = r_num3 % 1000
        digit_pos4 = r_num3 // 1000

        r_num5 = r_num4 % 100
        digit_pos5 = r_num4 // 100

        r_num6 = r_num5 % 10
        digit_pos6 = r_num5 // 10

        r_num7 = r_num6 % 1
        digit_pos7 = r_num6 // 1

    if column_number == 1:
        if digit_pos1 == 0:
            digit_pos1 = player_number
    elif column_number == 2:
        if digit_pos2 == 0:
            digit_pos2 = player_number 
    elif column_number == 3:
        if digit_pos3 == 0:
            digit_pos3 = player_number    
    elif column_number == 4:
        if digit_pos4 == 0:
            digit_pos4 = player_number
    elif column_number == 5:
        if digit_pos5 == 0:
            digit_pos5 = player_number
    elif column_number == 6:
        if digit_pos6 == 0:
            digit_pos6 = player_number
    elif digit_pos7 == 0:
            digit_pos7 = player_number
    else:
        print("invalid move")
        return

    if digit_pos1 > 0:
        print(digit_pos1,digit_pos2,digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos2 > 0:
        print(digit_pos2,digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos3 > 0:
        print(digit_pos3,digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos4 > 0:
        print(digit_pos4,digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos5 > 0:
        print(digit_pos5,digit_pos6,digit_pos7, sep='')
    elif digit_pos6 > 0:
        print(digit_pos6,digit_pos7, sep='')
    elif digit_pos7 > 0:
        print(digit_pos7)
    else:
        print("invalid move")
        return
    
main()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement