(20) Loop statement

Source: Internet
Author: User

* While loop
# while expression:
Statement (s)

Guess numbers
#!/usr/bin/env python
#-*-Coding:utf-8-*-

Import Random
Number = Random.randint (1,101)
Guess=0
While True:
Num_input=raw_input ("Please input one integer-is-1 to 100:")
Guess +=1 #统计猜的次数

If not Num_input.isdigit ():
print "Please input integer."
elif Int (num_input) <0 or int (num_input) >100:
Print "The number should in 0 to 100."
Else
If number = = Int (num_input):
Print "Ok,you is good. It's only%d and then you success. " %guess
Break #猜对了就跳出循环
Elif Number>int (num_input):
Print "Your number is more than less."
Elif Number<int (num_input):
Print "Your number is bigger"
Else
Print "Something is wrong."

[Email protected]:/var/www/erp-yejian# python test.py
Please input one integer-is-1 to 100:50
Your number is and less.
Please input one integer-is-1 to 100:75
Your number is bigger
Please input one integer-is-1 to 100:65
Your number is bigger
Please input one integer-is-1 to 100:55
Your number is and less.
Please input one integer-is-1 to 100:60
Your number is and less.
Please input one integer-is-1 to 100:63
Your number is and less.
Please input one integer-is-1 to 100:64
Ok,you is good. It's only 7 and then you success.

# Break and Continue
Break: Jump out of the loop
Continue: Skip this cycle into the next loop
Both of these are controlled circulation processes in the loop body

# while ... else ...
When you run to else, the loop body
#!/usr/bin/env python
#-*-Coding:utf-8-*-

Count=0
While count<3:
Print count, "is less than 3"
Count+=1
Else
Print count, "is isn't less than 3"

[Email protected]:/var/www/erp-yejian# python test.py
0 is less than 3
1 is less than 3
2 is less than 3
3 is isn't less than 3

* Powerful for
>>> mystr= "Hello"
>>> for I in MyStr:
... print I,
...
H e l l o
A letter and a letter printed out

>>> mylist=[11, ' good ', 22]
>>> for I in MyList:
... print I,
...
Good 22
Traverse List

>>> mytuple= (1, ' SS ', 2)
>>> for I in Mytuple:
... print I,
...
1 SS 2
Traversing tuples

>>> mydict={' first ': "Good", "second": "Better"}
>>> for I in Mydict.values ():
... print I,
...
Better good
Traverse Dictionary

>>> myset={' good ', ' better '}
>>> for I in MySet:
... print I,
...
Better good
Iterating through the collection

>>> for I in Range (2,8):
... print I,
...
2 3 4 5 6 7
Traverse the list produced by range

>>> [I for I in range (3,9) if i%2==0]
OUT[2]: [4, 6, 8]
Traversal in List parsing

>>> str1
OUT[16]: ' Hello '
>>> str2
OUT[17]: ' World '
>>> for x, y in Zip (str1,str2):
.. print x, y
...
H W
E o
L R
Dll
o D
and the zip function to use

* Iteration
# ITER
>>> MyList
OUT[42]: [' name ', 88]
>>> List_iter=iter (mylist)
>>> while True:
... print list_iter.next ()
...
Name
88
One-to-one output

(20) Loop statement

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.