In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "user input and while loop in Python how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "user input and while loop in Python how to use" this article.
I. user input
Input ()-the function input () temporarily stops the program and waits for the user's input. After getting the user's input, Python stores the input in a variable for later use.
1. Input string
Let's start with a simple example of user input.
Msg = input ('Please input a string:') # the content in the quotation marks should indicate what kind of information the user needs to provide. # if the prompt in the quotation marks has multiple lines, you can add'\ n'to wrap the line print ('The message you just input is "' + msg +'.')
The running result is shown in the following figure:
After the program runs, "Please input a string:" prompts the user to enter a string. After entering the string, the user presses the enter key to continue the program. The string entered by the user is stored in the variable msg, and the user input can be seen by printing msg.
two。 Use the function int () to get numeric input
Sometimes we need the user to enter a numeric value, but when using the function input (), Python interprets all the user input as a string, even if you enter a numeric value, you need to use the function int ().
Int ()-- the function int () converts the input numeric string to a numeric value.
User input processed by the function int () can be used as a numeric value.
The following is an example of using the function int () to get numeric input.
Age = input ('Please input your age:') age_integer = int (age) # converts a numeric string to a numeric value if age_integer > = 18: print ('Your age is over 18.') else: print (' Your age is under 18.')
The running result is shown in the following figure:
3. Application of Modulus operator
The modular operator% is often used in programming. it is used to return the remainder of the division of two numbers. If the remainder is 0, it can be divisible. By modulating the number entered by the user with 2, you can determine whether the number entered by the user is odd or even.
The following is an example of using a modular operation to determine whether the input is odd or even
Num = input ('Please input a number:') number = int (num) print ('The number you just input is' + num +'.) if number%2 = 0: # divisible by 2, even print ('It is an even number.') else: # not divisible by 2, odd print ('It is an odd number.')
The running result is shown in the following figure:
2. While cycle
The for loop, which is generally used for every element in the collection, has been introduced before, and the while loop runs until the specified condition is not met.
1. Simple while loop
Here is an example of a simple while loop:
Num = 1while num
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.