User input output And while loop

Source: Internet
Author: User
1. Using the function input (), print (), the string can be separated by commas. end= ' keyword parameter, you can print without wrapping, sep= ' you want the delimiter ', keyword parameters, replace the default delimited string.

2. Input output is output, so we collectively refer to Input/output, or abbreviated to IO. The interactive program can be written by acquiring user input and learning the running time of the control program.

3, the function input () lets the program pauses the operation, waits for the user to enter some text. After you get the user input, Python stores it in a variable to make it easier for you to use.

4. Whenever you use the function input (), you should specify clear and easy-to-understand hints that accurately indicate what information you want the user to provide-indicating that the user is prompted to enter any information.

5, you can store the hint in a variable, and then pass the variable to the function input ()

prompt = "If You have us who is, we can personalize the messages you see."

Prompt + = "\nwhat is your first name?" # string addition

Name = input (prompt)

operator + = Appends a string to the end of the string stored in prompt.

7.1 using int () to enter a number

Because input () returns a data type of str (string), Python converts the directly entered number into a string. STR cannot be directly compared to integers, and Str must first be converted to integers. Python provides an int () function to do this thing

Height = input ("How tall is, in inches?")

height = Int (height) #转化为整数

7.2 modulo operator

When working with numeric information, the modulo operator (%) is a useful tool that divides two numbers and returns the remainder:

>>> 4% 3

1

>>> 5% 3

2

>>> 6% 3

0

>>> 7% 3

1

If a number can be divisible by another number, the remainder is 0, so the modulo operator returns 0. You can use this to determine whether a number is odd or even.

7.3 While Loop

The For loop is used for a block of code for each element in the collection, while the while loop runs continuously until the specified condition is not satisfied.

1 Current_number = Current_number < 5:3     print (current_number) 4     current_number +=1

7.3.1 Use flag

True and False, Boolean operators and while loops are used together to implement complex logic.

7.3.2 using break to exit the loop

7.3.3 using continue in loops

1 current_number = 0 2  3 while Current_number < 10:4  5     current_number + 1 6  7     if current_number% 2 = = 0:8  9     continue10     print (current_number)

If the result is 0 (meaning that current_number can be divisible by 2), execute the Continue statement, let Python ignore the rest of the code, and return to the beginning of the loop. If the current number cannot be divisible by 2, the rest of the code in the loop is executed, and Python prints the number.

Loops are an effective way to make a computer do repetitive tasks.

The break statement can exit the loop directly during the loop, while the continue statement can end the cycle in advance and start the next cycle directly. These two statements usually have to be used with the IF statement.

Be especially careful not to misuse break and continue statements. Break and continue can cause too many logical forks for code execution and error-prone. Most loops do not need to use the break and continue statements, the above two examples can be removed by overwriting the loop condition or modifying the loop logic, and removing the break and continue statements.

7.3.4 Avoid infinite loops

Sometimes, if the code is written with a problem, it will cause the program to fall into a "dead loop", that is, forever loop. You can then use CTRL + C to exit the program, or force the end of the Python process.

7.4 Working with a while loop to work with lists and dictionaries

A For loop is an efficient way to iterate through a list, but the list should not be modified in the For loop, otherwise it will be difficult for Python to track the elements. To modify a list while traversing it, you can use the while loop. By combining the while loop with lists and dictionaries, you can collect, store, and organize large amounts of input for later viewing and display.

7.4.1 Moving elements between lists

By using the while loop, the function pops () Removes the end element, append () adds the element

7.4.2 Delete all elements that contain a specific value

Through the while loop and remove () to achieve

7.4.3 using user input to populate dictionaries

Responses[name] = response

Need to define a dictionary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.