'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


Solution 1:[1]

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()

Solution 2:[2]

The immediate cause of your problem is that, when parsing the 7 digit number manually, all your divisors are too small by a factor of 10. For instance,

r_num7 = r_num6 % 1
digit_pos7 = r_num6 // 1

should be

r_num7 = r_num6 % 10
digit_pos7 = r_num6 // 10

and all of the other quantities should also be ten times larger.


The much larger, overarching issue is that you're making coding much more difficult for you than it has to be. If you have a chain of elifs or you're copy-and-pasting long lists of variables, there's almost guaranteed to be a much simpler way to solve the problem in Python.

For instance, the entire 'digit position modular arithmetic' section of the code can be replaced by:

long_number = int(input())

# Pad to 7 digits with zeros
digits = [int(x) for x in str(long_number).zfill(7)]

If you have several variables with the same name except an increasing count appended to them, you should immediately think of using a list instead.

Here's a more Pythonic version of your entire algorithm:

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

# Pad to 7 digits with zeros
digits = [int(x) for x in str(long_number).zfill(7)]

if 1 <= column_number <= 7 and digits[column_number-1] == 0:
    digits[column_number-1] = player_number
    for i, digit in enumerate(digits):
        if digit > 0:
            print(''.join(map(str, digits[i:])))
            break
    else:
        print("invalid move")
else:
    print("invalid move")

Solution 3:[3]

The basic logic for any program is to exit with an error value. If you want to exit your code you can use sys.exit("Your_argument_here") or exit() or quit().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Dharman