Python Forum
May i ask how i can stop my coding running
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
May i ask how i can stop my coding running
#1
Good Day, may i ask how can i stop my code running. I am using if else statement.

try:
A = "111111"

if A == "123456":
print('OK')
else:
print('NG')

if A == "111111":
print('OK')
else:
print('NG')


if output get OK my code will continue run the next step.

If output get NG how i can stop running my code to the
next step
Reply
#2
It's unclear what 'try:' is doing here (is it part of try...except?).

To answer your question: you need to use elif instead of if. However, if you look at the code (you print out same messages) then it can be expressed more compactly:

if a in ['123456', '111111']:
    print('OK')
else:
    print('NG')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Oct-03-2019, 06:28 AM)perfringo Wrote: It's unclear what 'try:' is doing here (is it part of try...except?).

To answer your question: you need to use elif instead of if. However, if you look at the code (you print out same messages) then it can be expressed more compactly:

if a in ['123456', '111111']:
    print('OK')
else:
    print('NG')
try is the python syntax.
may i ask if my output get "NG" how i can stop running the code
Reply
#4
You can use the "exit" method from the "sys" module...
import sys

try: # Remember, a "try:" must always include an "except:" statement
    A = "111111"

    if A == "123456":
        print('OK')
    else:
        print('NG')
        sys.exit(1)

    if A == "111111":
        print('OK')
    else:
        print('NG')
        sys.exit(1)

except: 
    pass # Do nothing
OR (more Pythonicly) you can use that "try:, except:"...
try: # Remember, a "try:" must always include an "except:" statement
    A = "111111"

    if A == "123456":
        print('OK')
    else:
        raise ValueError("The value of 'A' ({}) was incorrect.".format(str(A)))

    if A == "111111":
        print('OK')
    else:
        raise ValueError("The value of 'A' ({}) was incorrect.".format(str(A)))

except ValueError as e:
    print("NG")

Quote:may i ask if my output get "NG" how i can stop running the code
A variation on perfringo's (correct) code...

while True:
    a = input("Enter a number...")
    if a in ['123456', '111111']:
        print("OK")
    else:
        print("NG")
        break # Will stop the infinite "while" loop
Reply
#5
You have been adviced two times also in previous Thread to use code tag @christing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stop if statement from running KieranPage 3 1,944 May-05-2020, 04:10 PM
Last Post: Yoriz
  my coding does not running, please help indora 2 2,270 Apr-20-2019, 10:41 AM
Last Post: indora

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020