Python second week problem sets (i)

Source: Internet
Author: User
Tags gcd greatest common divisor

# permutation combination C (n,m)

 m = Int (input ( " m =  "   = Int (input ( n =   " )) FM  = 1 for  num in  range (1, m + 1): FM  *= NUMFN  = 1for  num Span style= "COLOR: #0000ff" >in  range (1, n + 1 *= NUMFMN  = 1for  num in  range (1, m -N + 1 *= num  print  (FM//FN//FMN) 
deff (x):"""the factorial of the function x:p Aram x: Any positive integer: The factorial of the return:x"""y= 1 forZinchRange (1, x + 1): Y*=Zreturnyif __name__=='__main__': M= Int (Input ('m =')) n= Int (Input ('n ='))    #when you need to calculate factorial, instead of writing loops, just call the already defined function.     Print(f (m)//F (n)//F (m-n))

# Craps Game Improved version

 fromRandomImportRandintdefRoll_dice (n=2): Total=0 for_inchrange (N): Total+ = Randint (1, 6)    return Total#Change the name, select the name you want to change, press SHIFT + F6 fromRandomImportRandintmoney= 1000 whileMoney >0:Print('Total player assets:%d'%Money ) whiletrue:debt= Int (Input ('Clear bet:'))        if0 < Debt <=Money : Breakgo_on=FalsePrint('player has rolled out%d'%Roll_dice ())ifRoll_dice () = = 7orRoll_dice () = = 11:        Print('player wins') Money+=DebtelifRoll_dice () = = 2orRoll_dice () = = 3orRoll_dice () = = 12:        Print('player loses') Money-=DebtElse: go_on=True whileGo_on:roll_dice ()Print('player has rolled out%d'%Roll_dice ())ifRoll_dice () = =Roll_dice ():Print('player wins!') Money+=Debt go_on=FalseelifRoll_dice () = = 7:            Print('player loses!') Money-=Debt go_on=FalsePrint('you are bankrupt! ')

# Judging Prime numbers

 fromMathImportsqrtdefis_prime (num): forFactorinchRange (2,int (sqrt (num)) + 1):        ifNum% factor = =0:returnFalsereturnNum!=1 andTrueorFalse#The following if condition allows you to import the module without executing the code below. #Ctrl + left mouse button to find explanation definition. if __name__=='__main__': Num= Int (Input ('Please enter a number to determine if it is prime:'))    Print(Is_prime (num))

# least common multiple and greatest common divisor

defLCM (x, y):#if x > y:    #(x, y) = (y, x)    " "least common multiple:p Aram x: Any positive integer:p Aram y: Any positive integer: return:x and y least common multiple" "(x, y)= (y, x)ifX > YElse(x, y) forIinchRange (y, x * y + 1):        ifI% x = = 0 andI% y = =0:returnIdefgcd (x, y):#if x > y:    #(x, y) = (y, x)    #Document Comments    """calculate Greatest Common Divisor:p Aram x: Any positive integer:p Aram y: Any positive integer: return: Greatest Common divisor"""    #(x, y) = (y, x)ifX > YElse(x, y) I=x whilei >0:ifX% i ==0 andY% i = = 0:#range (x, 0,-1) from large to small.             returnI i-= 1if __name__=='__main__': x= Int (Input ('Please enter the first number x=')) Y= Int (Input ('Please enter a second number y='))    Print('least common multiple:', LCM (x, y))Print('Greatest common divisor:', gcd (x, y))

# Judging Palindrome prime numbers

 fromMathImportsqrtdefis_prime (num): forFactorinchRange (2,int (sqrt (num)) + 1):        ifNum% factor = =0:returnFalsereturnNum!=1 andTrueorFalsedefis_palindrome (num):" "Judging is not a palindrome number:p Aram num: nonnegative integer: Return: is a palindrome number returns true, not a palindrome number returned flase. " "Temp=num Total=0 whileTemp >0:total= Total * + temp% 10Temp= 10returnnum = = Total#both and and or operators are operators with short-circuit functionality#if the expression on and to the left is flase, then the expression on the right is shorted#if the expression on or to the left is true, then the expression on the right is shorted#So the order in which the left and right sides of the expression is placed can have a noticeable effect on execution efficiency. if __name__=="__main__": Num= Int (Input ('Please enter a number:'))    Print(Is_palindrome (num))ifIs_palindrome (num) andprime_number (num):Print('% is palindrome prime number'%num)Else:        Print('%f is not a palindrome prime .'% num)

# A Match game, who gets the last one, who loses. The range of each fetch [1,4]. Make sure the computer wins.

defGame_match (): Total= 21 whileTrue:my_number= Int (Input ('Please enter the number of matches you have taken:'))        if1 <= my_number <= 4: Com_number= 5-my_number Total-= 5Print('How many sticks are there in the computer?', total)ifTotal = = 1:            Print('you lost.')             Break        Else:            Print('Please enter the correct number of matches!')if __name__=='__main__': Game_match ()

# 21 Match Games Fair edition

 fromRandomImportRandintdefMain (): Total= 21 whileTotal >0:Print('there's%d matches left .'%Total ) whileTrue:num= Int (Input ('Please enter the number of matches you want to take:'))            if1 <= num <= 4 andNum <=Total: Break Total-=NumPrint('when you're done, there's%d left .'%Total )ifTotal >0:com= Randint (1,min (4, total)) Print('the computer got%d matches.'%com) total-=comifTotal = =0:Print('The computer took the last match and the computer lost it! ')                 Break        ifTotal = =0:Print('you took the last match, you lost 1.')if __name__=='__main__': Main ()

Python second week problem sets (i)

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.