Python's While loop statement (instance)

Source: Internet
Author: User

#While Loopnumbers=[1,2,4,6,7,8,12]enent=[]odd=[] whileLen (Numbers) >0:number=Numbers.pop ()if(number% 2 = =0): enent.append (number)PrintNumber'is even'        PrintnumbersElse: odd.append (number)PrintNumber'not even'PrintenentPrintOdd#continue and break usage#Continue is used to skip the cycle,#Break is used to exit the loop,#In addition, the "judging condition" can also be a constant value, indicating that the loop must be establishedi = 1 whileI < 10: I+ = 1ifI% 2 > 0:#Skip output when non-even        Continue    PrintI#output even 2, 4, 6, 8, tenI= 1 whileTrue:#the cycle condition of 1 must be set    PrintI#Output 1~10i + = 1ifI > 11:#when I is greater than 10 o'clock jump out of the loop 

Example 1:

  1, scissors small game

#!/usr/bin/python#-*-coding:utf-8-*-ImportRandom while1: S= Int (Random.randint (1, 3)) ifs = = 1: IND="Stone" elifs = = 2: IND="Scissors" elifs = = 3: IND="cloth"m= Raw_input ('Enter stone, scissors, cloth and end game by entering "end":') blist= ['Stone',"Scissors","cloth"] if(M not inchblist) and(M! ='End'): Print "input Error, please re-enter! " elif(M not inchblist) and(M = ='End'): Print "\ nthe game exits ..." Break elifm = =IND:Print "The computer went out:"+ IND +", Draw! " elif(M = ='Stone' andIND = ='Scissors')or(M = ='Scissors' andIND = ='cloth')or(M = ='cloth' andIND = ='Stone'): Print "The computer went out:"+ IND +", you won! " elif(M = ='Stone' andIND = ='cloth')or(M = ='Scissors' andIND = ='Stone')or(M = ='cloth' andIND = ='Scissors'): Print "The computer went out:"+ IND +", you lost! "

Result output:
Enter stone, scissors, cloth, enter  end game: Stone computer out: Stone, Draw! Enter stone, scissors, cloth, enter  end game: Stone    computer out: Scissors, you won! Enter stone, scissors, cloth and end game by entering "end" :

Example 2:

2. Dice Game #Coding=utf-8#!/usr/bin/pythonImportRandomImportSYSImportTimeresult= []#print Type (INSS) whileTrue:inss= Int (Raw_input ("Enter a 1 to start the dice:"))    ifINSS ==1: result.append (int (Random.uniform (1,7)) ) result.append (int (random.uniform ) (1,7)) ) result.append (int (random.uniform ) (1,7)))        PrintResult Count=0 Index= 2Pointstr=""         whileIndex >=0:currpoint=Result[index] Count+=Currpoint Index-= 1#pointstr =            #Pointstr + = ","            #pointstr +=str (POINTSTR)            #Print str (currpoint)            #Print Pointstr        ifCount <= 11:            PrintResult"- Small"Time.sleep (1)#Sleep One second        Else:            PrintResult"-- Large"Time.sleep (1)#Sleep One second    Else:        Print "you typed the wrong character."result= []

Execution Result:
Enter a 1, start the dice: 1[1, 5, 6] [1, 5, 6]-- big input a 1, start the dice:2 The characters you typed are not right oh enter a 1 and start the dice:

Python's While loop statement (instance)

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.