+ 2

[SOLVED] if condition

Hi, I’m new to python and I was wondering if there is any thing I can input if someone is underage or doesn’t meet the requirements, how do I stop it from proceeding to the next thing?

11th Feb 2022, 12:16 AM
David Proseanchin
17 Respostas
+ 3
Im just practicing, but here it is: 1. Name = input(“Give me your name: “) 2. Print(“welcome “ + name) 3. age = int(input(“Please enter a persons age:”)) 4. If age > 13: print(‘unlocked’)
11th Feb 2022, 12:57 AM
David Proseanchin
+ 2
Ok thank you!
11th Feb 2022, 1:06 AM
David Proseanchin
+ 2
â–ȘIn LOOPS, you can check a condition and use the "break" statement whenever you need to immediately terminate the loop. â–ȘIf the running SCRIPT/PROGRAM resides in a function, you can use "pass" or "return" (without arguments) to terminate the function altogether. ○ Examples: # let's say 18+ is a legal adult & anything less is underage ... ---- example 1 ---- def examplefunc(age): if age < 18: return # SCRIPT will terminate if age isn't 18+ # or can use pass, but not advised else: print('is old enough, continue w/ logic.') ---- example 2 ---- # assume "people" is a group attempting to watch an 18+/R movie @ cinema people = [ ('Fox', 28), ('Kai', 19), ('rando', 17) ] for person in people: name, age = person # unpack if age < 18: print('Go home, %s -- underage!' % name) break # LOOP will terminate due to "rando" being 17 (not 18+) else: print('enjoy movie')
12th Feb 2022, 6:34 PM
Fox
Fox - avatar
+ 2
Sigh. I should learn to read all of the answers before sharing my answers. SMH. RE: "I want to create something that asks for your name and age, and if you dont pass the age requirement, it stops you." â–ȘExample: def example(): requirement = input("ENTER MINIMUM AGE REQUIRED: ") requirement = int(requirement) # remember that anything submitted to the input function will always be a string by default ... so you must convert it into an integer name = input("ENTER NAME: ") age = int(input("ENTER AGE: ")) # taking age & converting it to an integer in a single step if age < requirement: print('%s is underage -- terminating' % name) return else: print('%s is old enough' % name) print('replace this print statement with code') ---- Let me know if this helped. ^^
12th Feb 2022, 7:23 PM
Fox
Fox - avatar
+ 2
RE: "What does it mean to write my code inside the if statement?" > The code inside of an "if statement" is executed if the condition being checked returns True. By "the code inside of an if statement" -- the executed code (of a True if conditional) can be found nested under the condition being checked ... and you'll notice that it's all indented (usually via the use of the TAB key on a keyboard). Indentation is very important in Python.
12th Feb 2022, 7:30 PM
Fox
Fox - avatar
+ 1
RE: "My last question is if they dont meet the sge requirments, is there code to stop them from automatically moving tot he next line?" > Do you mean something like the following ... ? # define a list of tuples containing names associated with ages people = [ ('Fox', 28), ('Austin', 42), ('Tabitha', 16), ('Caleb', 9), ('Kit', 82) ] # define two empty lists old_enough = [] underage = [] # Loop through the list of people. If they are underage, append their name to the underage list. If they are an adult (18+), append their name to the old_enough list. for person in people: name, age = person # unpack if age < 18: underage.append(name) else: old_enough.append(name) print('loop finished') # Output the results (lists' contents) print('UNDERAGE: ' + str(underage)) print('ADULTS: ' + str(old_enough))
12th Feb 2022, 6:59 PM
Fox
Fox - avatar
0
Yeah, i found a post on it. The code i found was: if age > 1 and age > 13: print(“Unlocked.”) else: print (“denied.”) My question is when i run it, it says both words.
11th Feb 2022, 12:23 AM
David Proseanchin
0
I changed it to not move unless you are over 13, but when i enter and age lower like 5, it skips over it and goes to the next line
11th Feb 2022, 12:50 AM
David Proseanchin
0
Sorry, could you give me an example, I just started coding this week. Thank you!
11th Feb 2022, 12:53 AM
David Proseanchin
0
What does it mean to write my code inside the if statement?
11th Feb 2022, 12:53 AM
David Proseanchin
0
It want to basically create something simple that asks for your name and age, and if you dont pass the age limit, it stops you. I will look at the beginner Python too :)
11th Feb 2022, 12:59 AM
David Proseanchin
0
Im getting an error saying File “<string>”, line 8, in <module> NameError: name ‘sys’ is not defined
11th Feb 2022, 1:11 AM
David Proseanchin
0
Never mind, simple typo, thank you for everything
11th Feb 2022, 1:12 AM
David Proseanchin
0
I hate having to ask too many questions, but after i enter an age like 14 or higher, it terminates the terminal?
11th Feb 2022, 1:14 AM
David Proseanchin
- 1
Ok, thank you! My last question is if they dont meet the sge requirments, is there code to stop them from automatically moving tot he next line?
11th Feb 2022, 12:38 AM
David Proseanchin
- 1
Even when the conditions arent fulfilled, it moves to the next line?
11th Feb 2022, 12:48 AM
David Proseanchin