+ 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?
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â)
+ 2
Ok thank you!
+ 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')
+ 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. ^^
+ 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.
+ 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))
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.
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
0
Sorry, could you give me an example, I just started coding this week. Thank you!
0
What does it mean to write my code inside the if statement?
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 :)
0
Im getting an error saying File â<string>â, line 8, in <module>
NameError: name âsysâ is not defined
0
Never mind, simple typo, thank you for everything
0
I hate having to ask too many questions, but after i enter an age like 14 or higher, it terminates the terminal?
- 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?
- 1
Even when the conditions arent fulfilled, it moves to the next line?