Conditional statement and while LOOP, conditional statement while loop

Source: Internet
Author: User

Conditional statement and while LOOP, conditional statement while loop
I. conditional statement if ...... Else

1. Basic if statement

Example:

If the age of a person is greater than or equal to 18, "adult" is output; otherwise, "not adult" is output"

#! /Usr/bin/env python #-*-coding: utf8-*-age_of_man = 20if age_of_man> = 18: print ("adult") else: print ("not adult ")

2. if supports nesting

If condition 1: indented code block elif condition 2: indented code block elif Condition 3: indented code block... else: indented code block

 

If the score is greater than or equal to 90, the score is excellent. If the score is greater than or equal to 80 and <90, the score is good. If the score is greater than or equal to 60 and <80, the score is qualified. Otherwise, it is unqualified.

#! /Usr/bin/env python #-*-coding: utf8-*-score = input (">>:") score = int (score) if score >=90: print ("excellent") elif score> = 80: print ("good") elif score> = 60: print ("qualified") else: print ("unqualified ")

 

Ii. while Loop
While condition: # loop body # If the condition is true, the loop body is executed. After the execution is complete, the loop body repeats again and then judges the condition again... # If the condition is false, the loop body is not executed and the loop ends.

 

# Print 0 to 10 #! /Usr/bin/env python #-*-coding: utf8-*-count = 0 while count <= 10: print (count) count + = 1

 

 

Endless loop

#!/usr/bin/env python# -*- coding:utf8 -*-while 1 == 1:    print("ok")

 

 

Exercise

1. Use the while loop to output 1 2 3 4 6 7 8 9 10

# Method 1 count = 1 while count <11: if count = 5: pass else: print (count) count = count + 1 # method 2 n = 0 while n <11: if n = 5: n = n + 1 continue print (n) n = n + 1

 

2. Output an odd number less than 100

count = 1while count < 101:    temp = count % 2    if temp == 0:        pass    else:        print(count)    count = count + 1

3. Calculate the sum of numbers 1 to 100

n = 1s = 0while n <101:    s = s + n    n = n + 1print(s)

4. Calculate 1-2 + 3-4 + 5-6 ...... + 99

n = 1s = 0while n < 100:    temp = n % 2    if temp == 0:        s = s - n    else:        s = s + n    n = n + 1print(s)

5. User Login (there are three retries)

Count = 0 while count <3: user = input ('>>>') pwd = input ('>>> ') if user = 'reese 'and pwd = '000000': print ('Welcome login') break else: print ('username or password error') count = count + 1

 

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.