[week6-7]
Creating Conditional Program Flow in Python
(1)(2)
Example 1
Write a program that takes a number as input and prints True if the
number is greater than 10 or less than 1; otherwise, print False.
(Use comparison and logical operators. Do NOT use an if statement.)
2
Example 2
There is a ride that only allows people whose height is between 100 cm
and 180 cm.
Write a program that takes the user's height (in cm) as input and prints
True if they are allowed to enter; otherwise, print False.
(Use comparison and logical operators. Do NOT use an if statement.)
3
Example 3
▪ Installing the pandas Library
• Since pandas is an external library, it needs to be installed separately.
To use pandas in the IDLE editor, you need to install it using the pip command.
• First, press the Windows key on your keyboard, type cmd, and open the Command Prompt
window.
➢ Then, in the Command Prompt, type the following command: pip install pandas
➢ The installation will proceed automatically.
➢ As shown in the image below, check the message confirming that the pandas version has been successfully
installed.
4
Example 3. Conditional python coding
▪ Searching Instagram Rankings
• In Python, you can use an if statement to create conditional flow.
0
< When the follower count is entered correctly>
< When the follower count is entered incorrectly>
5
Example 4
Write a program that takes a user ID as input and checks
whether it matches the ID stored in the program.
Print the result accordingly.
login.py
id = "ilovepython"
s = input(“Enter the ID: ")
if s == id:
print(“Welcome")
else:
print(“ID not found. ")
Enter the ID: ilovepython
Welcome
< Python code>
Enter the ID: iloveruby
ID not found.
6
Example 5. Nested if statement examples
❖ If the score is 85, what is the grade?
소스 Code
실행 결과
7
Example 6
Upgrade Problem 4. Write a program that takes both an ID and a
password as input.
If both match (ID: ilovepython, Password: 123456), print “Welcome”.
8
Example 7
(Continue next slide)
9
Example 7
Searches up to the 3rd rank (third row)
< When the follower count is entered correctly>
< When the follower count is entered incorrectly>
10