Python learning day 2, python Day 2

Source: Internet
Author: User

Python learning day 2, python Day 2
Note: writing a program cannot write repetitive code learning content: data type, for loop, while loop, character encoding, File Processing 1, for loop 1, simple for loop for I in range (10): # Here: A set of data in the loop range, that is, print (I) 10 times in a loop # print the I value each time. 2. A simple age-based game in a for loop #! /Usr/bin/env python #-*-coding: UTF-8-*-#-*-coding: UTF-8-*-oldboy_age = 40for I in range (4 ): guess_age = int (input ("Enter the estimated age:") # define the age data type if guess_age = oldboy_age: print ("Congratulations, you guessed it! ") # The printed information break after the guess # After the guess, you need to jump out of the current loop elif guess_age> oldboy_age: print (" sorry, I guess it's big. ") else: print (" Sorry, you can guess it! ") Else: # If the for loop ends normally, execute the statements below else. Exit ("operation four times, loop end") print ("You are so smart") Note 1: multiple breaks are allowed in the loop, but once the current loop exists, the program continues and cannot return. In addition, break can only be used in a loop. If there is no loop, an error occurs when break is used. NOTE 2: exit () is the method to exit the program. For example, you can add "exit" at the end after multiple operations. ("You have performed too many operations, forced to exit... ") 3. upgraded version of the for Loop: Nested for loop for I in range (10): for j in range (8): if j <5: # break usage: jump out of the current layer loop and skip only one continue # usage of continue: jump out of the current loop and continue the next loop print (I, j)

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.