![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
i = 10
if (i > 10):
print("i is less than 10")
print("i does not meet the condition")
Create the Python script with the command:
nano ifelse.py
python3 ifelse.pyThe output will be:
i does not meet the conditionLet’s make this a bit more useable. What we’ll do is allow input from the user. For this, we’ll define i using the float() method, which returns a floating point number from a string. This is required because users could input a decimal point value. Without float(), should a user input a decimal, the app would fail. The float() line looks like this:
i = float(input ("Enter a number: "))
Next, we’re going to employ the full if-else statement, so we’ll first have an if statement and then an else statement, each of which will use the print() function to output a different string of text. The full script looks like this:
i = float(input ("Enter a number: "))
if i < 10:
print("i is less than 10")
else:
print("i does not meet the condition")
Create the script with:
nano ifelse2.pyPaste the above script and then save the file. Run the script with the command:
python3 ifelse2.pyYou will be required to input a number. So long as the number (even with a decimal point value) is less than 10, the output will be:
i is less than 10If the input is more than 10 (else), the output will be:
i does not meet the conditionLet’s create a script with a compound test. Our test will be for age and height to meet the requirements to ride a roller coaster (which requires riders to be over 12 years old and be at least 48 inches tall). First, we’ll define the age and height variables like so:
age = int(input("Enter your age: "))
height = int(input("Enter your height in inches: "))
Next, comes the if-else statement that will use our variables. The trick is that our if statement will use a compound test which looks like this:
if age > 12 and height > 48:After the if statement, we use the print() function and print out Enjoy the ride. Our else statement will have the sad job of printing out Sorry, but you cannot enjoy this ride. The full script looks like this:
age = int(input("Enter your age: "))
height = int(input("Enter your height in inches: "))
if age > 12 and height > 48:
print("Enjoy the ride.")
else:
print("Sorry, but you cannot enjoy this ride.")
Create a new file with:
nano ride.pyPaste the script and save/close the new file. Run the script with:
python3 ride.pyEnter your age and then enter your height. If you meet both requirements, you’ll see Enjoy the ride, otherwise (else) you’ll see Sorry, but you cannot enjoy this ride. If you feel you’ve mastered Python, this is the end of your studies. Else… come back next week for more Python goodness.