Getting started with Python--19--else statements, with statements

Source: Internet
Author: User

1, else with while:

X=input (' Please output an integer: ')

While X>0

X=x-2

Print (x)

Else

Print (' x is already less than or equal to zero! ‘)

2, else with try:

X=input (' Please enter: ')

Try

print (int (x))

Except ValueError as reason:

Print (' Error ' +str (reason))

else: #如果程序报错, the output is the letter of things, that output only error message, else part will not output

Print (' no exception ') #如果没有报错, the output is int (x) and ' no exception ', that is, else also outputs

The following says with

The WITH statement is primarily used with open:

With open (' Xx.txt ') as F:

Print (F.read ())

But when you no longer need this file, or have already read it, it can be closed, it will help you to automatically close, do not need you manually close

Getting started with Python--19--else statements, with statements

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.