2

I have been trying to write this code and I need a way of stopping the code if a certain input is incorrect, here is what I have so far:

first_name = input('PLEASE ENTER YOUR FIRST NAME: ')
last_name = input('PLEASE ENTER YOUR SURNAME: ')
print(f'Hello {first_name} {last_name}, before you enter.')

def age_det():
    age = input(f'How old are you?')
    converted_age = [int(age)]
  
    for num in converted_age: 
      while num>=18:
        print(f'Awesome, {first_name}, welcome to Blablabla')
        num += 100
      while num <= 18:
        break
      print(f'Sorry, {first_name}, but we require you to be at least 18 to enter Blablabla.')
      num += 100

age_det()
#I want the code to stop here if the age entered is under 18    

username = input('Before we start, please pick a username: ')

print(f'Woah! {username}, good choice!')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
alexshakesm
  • 23
  • 1
  • 3
  • 4
    `if age < 18: sys.exit(1)` ... – costaparas Jan 10 '21 at 12:55
  • 1
    it was previously answered at : https://stackoverflow.com/questions/543309/programmatically-stop-execution-of-python-script and https://stackoverflow.com/questions/73663/how-to-terminate-a-python-script – khalilolgod Jan 10 '21 at 13:12
  • Does this answer your question? [How do I abort the execution of a Python script?](https://stackoverflow.com/questions/179369/how-do-i-abort-the-execution-of-a-python-script) – costaparas Jan 10 '21 at 13:18

2 Answers2

1

Import the sys object and use an if statement to check if the age is valid. If not call sys.exit().

import sys

age = 17
if age < 18:
    sys.exit()

print("Still going") # Doesn't get printed

if the age is at least 18:

import sys

age = 25
if age < 18:
    sys.exit()

print("Still going") # Gets printed
Honn
  • 737
  • 2
  • 18
0

First import these :-

import sys

Then add this after that "sorry we don't allow under 18" line

input('Press Enter to Continue...')
sys.exit()

Plus use if instead of while

Your code can be simplified as :-

import sys

first_name=input('PLEASE ENTER YOUR FIRST NAME: ')
last_name=input('PLEASE ENTER YOUR SURNAME: ')
print(f'Hello {first_name} {last_name}, before you enter.')

def age_det():
    age = int(input(f'How old are you?'))
    if num>=18:
        print(f'Awesome, {first_name}, welcome to Blablabla')
    else :
        print(f'Sorry, {first_name}, but we require you to be at least 18 to enter Blablabla.')
        input('Press Enter to Continue...')
        sys.exit()

age_det()
0xB00B
  • 1,598
  • 8
  • 26
  • Why you need to do `as exiter`? – ppwater Jan 10 '21 at 13:03
  • 1
    @ppwater Because if I directly do `from sys import exit` then it interfares with built-in `exit()` . So I imported it as exiter. You can use name of you choice. – 0xB00B Jan 10 '21 at 13:10
  • 1
    Ah. okay. I think you can just do `import sys` `sys.exit()` though. importing with `from` is not good IMHO.. – ppwater Jan 10 '21 at 13:17
  • @ppwater yes I know that. But why import whole `sys` when you only need `sys.exit`. Importing with `from` doesn't cause any problems. It's perfectly normal – 0xB00B Jan 10 '21 at 13:28
  • `import exit as exiter` is totally unnecessary and very bizarre. `import sys` then call `sys.exit()` is all you need. Renaming key imports unnecessarily leads to confusing code and bugs. – smci Jan 10 '21 at 13:28
  • 1
    I've never seen it been done this way. `sys` is routinely imported as `import sys`. You often need `sys.path`, `sys.argv` etc. – costaparas Jan 10 '21 at 13:31
  • Alright, if you so much hate exiter(), I updated answer to use sys.exit(). – 0xB00B Jan 10 '21 at 13:38