In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use the judgment sentence in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
# input () function
# this function is used to get the user's input
After # input () is called, the program pauses immediately and waits for user input
# after the user has entered the content, click enter before continuing to execute
# after the user's input is completed, the input will be returned in the form of return value
# Note: the return value of input () is a string
A string can be set as an argument in the # input () function, which will be displayed as prompt text
A = input ('Please enter anything:') print ('user input is:', a)
# input () can also be used to temporarily prevent the program from ending
# if-else statement
# Syntax:
If conditional expression: code block else: code block
# execution process:
When # if-else statement is executed, the conditional expression after if is evaluated first.
# if True, execute the code block after if
# if False, execute the code block after else
# if-elif-else statement
# Syntax:
If conditional expression: code block elif conditional expression: code block else: code block
# execution process:
When the # if-elif-else statement is executed, it evaluates the conditional expression from top to bottom.
# if the result of the expression is True, the current code block is executed and the statement ends
# if the result of the expression is False, continue to judge until True is found
# if all expressions are False, then execute the code block after else
# only one code block in if-elif-else will execute
Code case
# =
# function: conditional judgment statement
# author: brother python
# time: 2020-6-24
# Wechat official account: DJXY00001
# Wechat name: Python Promotion Class
# Version:1.0
# = = # the first part of the example # get the user name username = input ('Please enter your user name:') # determine whether the user name is adminif username = = 'admin': print (' Welcome administrator!') # part II example # Let the user enter an instance flag = int (input ('do you love your girlfriend) in the console? 1 for love, 0 for dislike:) # if the user enters 1if flag = = 1: print ('you are a good kid ~') else: print ('you play hooligan ~') # the third part of the example # lets the user enter an age age = int (input ('enter your age:') if age > 200: print ('it's boring to be alive!') Elif age > 100: print ('you are too old and too young!') Elif age > = 60: print ('you are retired!') Elif age > = 30: print ('you are middle-aged!') Elif age > = 18: print ('you are an adult!') Else: print ('you are still a child!')
Code run output:
Please enter your user name: admin Welcome the administrator! Do you love your girlfriend? 1 stands for love, 0 means not love: 0 you play hooligans ~ ~ Please enter your age: 52 you are middle-aged!
Program analysis
In the third line of the program, through the input function, waiting for user input, the program assigns the information entered by the user to the variable username, and the fifth line determines the output.
Line 9 is waiting for user input. It defaults to character type, which is converted to numeric type by int (), and judged by lines 11 to 14, which reflects the usage of if-else.
Lines 17 to 29, which are also specific judgments after the user enters the age, reflect the use of if-elif-else
On how to use judgment sentences in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.