10. 判斷(if-elif-else)和循環(huán)(Loop)的綜合練習

綜合練習

# 0.前置知識

'''

a_list = [1,2,3]

print(sum(a_list))

'''

'''

import random ????#python內(nèi)置的庫,用于生成隨機數(shù)

point1 = random.randrange(1,7)

point2 = random.randrange(1,7)

point3 = random.randrange(1,7)

print(point1,point2,point3)

'''

# 1.寫一個用戶猜大小的游戲,并根據(jù)輸入的數(shù)據(jù)輸出相應(yīng)的結(jié)果

答案:個人版

'''

Demo 10 寫一個猜大小的游戲import random

def shake_dce():

????print("<<<<>>>>")

????number = input("Big or Small:")

????list = []

????for dce in range(1,4):

????????dce = random.randrange(1,7)

????????list.append(dce)

? ? ????list_all = list[0]+list[1]+list[2] #list下標從0開始

????????if (11 <= list_all <= 18) and number=="Small":

????????????print("You Lose!")

????????elif (11 <= list_all <= 18) and number=="Big":

????????????print("You Win!")

????????elif (3<=list_all<=10) and number=="Small":

????????????print("You Win!")

????????elif (3<=list_all<=10) and number=="Big":

????????????print("You Lose")

????????else:

????????????print("Wrong Tings")

????????????print("<<<<>>>>")

????????????print("The points are " +str(list))

shake_dce()

'''

答案:參考答案版

import random

def roll_dice(numbers = 3 , points = None): #numbers:篩子數(shù)量 points:點數(shù)列表

? ? print("<<< ROLL THE DICE ! >>>")#告知用戶搖篩子

? ? if points is None :#參數(shù)中未指定points,為points創(chuàng)建一個空列表

? ? points = []

????while numbers > 0:

????????point = random.randrange(1,7)

????????points.append(point)

????????numbers = numbers - 1#搖動篩子三次,每搖動一次數(shù)量-1,直到小于0

????return points#返回結(jié)果列表

def roll_result(total):#創(chuàng)建參數(shù),必要的參數(shù)是篩子的總點數(shù)

? ? isBig = 11 <= total <= 18 #定義”大“的標準

? ? isSmall = 3 <= total <=10#定義”小“的標準

? ? if isBig:

? ? ? ? return 'Big'

????elif isSmall:

? ? ????return 'Small' #在不同的條件下返回不同的結(jié)果

def start_game():#建立函數(shù)

????print("<<< GAME STARTS !>>>")#告知用戶游戲開始

? ? choices = ['Big','Small']#規(guī)定正確的輸入

? ? your_choice = input("Big or Small :")#用戶輸入的字符放在your_choice中

? ? if your_choice in choices:#如果符合規(guī)定就繼續(xù)向下執(zhí)行

? ? ? ? points = roll_dice()##調(diào)用roll_dice函數(shù),返回列命名為points

? ? ? ? total = sum(points)#點數(shù)求和

? ? ? ? youWin = your_choice = roll_result(total)#所選結(jié)果和計算機所選結(jié)果相同

? ? ? ? if youWIn:#成立告知勝利

? ? ? ? ? ? print('The points are',points,'you win !')

? ? ? ? else:#不成立告知失敗

? ? ? ? ? ? print(' The points are',points,'You lose !')

? ? else:#輸入不符合規(guī)定則告知不符合規(guī)定

? ? ? ? ?print(”Invalid Words“)

? ? ? ? start_game()

start_game()

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


1.課后練習題1

#2.

# Demo 11 加上功能:下注金額和賠率

#初始金額 1000元

#金額為 0 時游戲結(jié)束

#押對了會獲得相應(yīng)的金額,押錯了會輸?shù)粝鄳?yīng)的金額

import random

def roll_dice(numbers =3,points =None):#numbers:篩子數(shù)量 points:點數(shù)列表

? ? print("<<<ROLL THE DICE! >>>")#告知用戶搖篩子

? ? if pointsis None:#參數(shù)中未指定points.為points創(chuàng)建一個空列表

? ? ? ? points = []

????while numbers >0:

????????point = random.randrange(1,7)

????????points.append(point)

????????numbers = numbers -1? #搖動三次篩子,每搖動一次數(shù)量-1.直到小于等于0

? ? ????return points#返回結(jié)果列表

def roll_result(total):#創(chuàng)建參數(shù),必要的參數(shù)是篩子的總點數(shù)

? ? isBig =11 <= total <=18 #定義‘大’的標準

? ? isSmall =3 <= total <=10 #定義‘小’的標準

? ? if isBig:

????????return 'Big'

? ? elif isSmall:

????????return 'Small' #在不同的條件下返回不同的結(jié)果

def start_game():#創(chuàng)建函數(shù)

? ? your_money =1000 #本金1000元

while your_money >0:

????????print('<<<<< GAME STARTS !>>>>>')#告知用戶游戲開始

? ? ? ? choices = ['Big','Small']#規(guī)定正確的輸入

? ? ? ? your_choice =input("Big or Small :")#用戶輸入的字符串放在 your_choice中

? ? ? ? your_bet_money =int(input('How money you wanna bet? -'))#輸入想要進行打賭的金額

? ? ? ? if your_bet_money <=1000:#參與打賭的錢要小于本金

? ? ? ? ? ? if your_choice in choices:#如果符合規(guī)定繼續(xù)向下執(zhí)行

? ? ? ? ? ? ? ? points = roll_dice()###調(diào)用roll_Dice函數(shù),返回的列命名為points

? ? ? ? ? ? ? ? total =sum(points)#點數(shù)求和

? ? ? ? ? ? ? ? youWin =your_choice = roll_result(total)#所選結(jié)果和計算機生成結(jié)果相同

? ? ? ? ? ? ? ? if youWin:#成立告知勝利

? ? ? ? ? ? ? ? ? ? print('The points are',points,'You win !')

????????????????????money_now = (your_bet_money*2)+(your_money-your_bet_money)

????????????????????print(' you gained ' +str(your_bet_money)+','+' you have '+str(money_now) +' know ' )#獲得打賭贏得的錢

? ? ? ? ? ? ? ? ? ? your_money = money_now

????????????????else:#不成立告知失敗

? ? ? ? ? ? ? ? ? ? print('The points are',points,'You lose !')

????????????????????money_now = ((-1)*your_bet_money)+ (your_money - your_bet_money)

????????????????????print(' you lost ' +str(your_bet_money) +',' +' you have ' +str(money_now) +' know ')# 失去打賭輸?shù)玫腻X

? ? ? ? ? ? ? ? ? ? your_money = money_now

????????????else:#不符合規(guī)定則提示不符合規(guī)定

? ? ? ? ? ? ????? ? print('Invalid Words')

????????????????????#start_game()

? ? ? ? else:#大于本金會提示錢不夠

? ? ? ? ? ? print('Invalid Money')

else:#退出游戲

? ? print("GAME OVER")

start_game()


2.課后練習2

CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]

CN_union = [130,131,132,155,156,185,186,145,176,1709]

CN_telecom = [133,153,180,181,189,177,1700]

CN_aLL = CN_mobile + CN_union + CN_telecom

#print(CN_aLL)

for i in CN_aLL:

????string =str(input("Enter Your Number :"))# 告知用戶輸入數(shù)字

? ? first_three =int(string[0:3])#字符串前3位,要轉(zhuǎn)換成int類型

? ? first_four =int(string[0:4])#字符串前4位,要轉(zhuǎn)換成int類型

? ? if(len(string) ==11):#print("valid length")

? ? ? ? if first_threein CN_mobileor first_fourin CN_mobile:#或者條件 用 or連接

? ? ? ? ? ? ? ? print("China Moile")

????????????????print("We're sending verification code via test to your phone:"+ string)

????????????????break #找到后用break跳出,不然會循環(huán)多次

? ? ? ? elif first_threein CN_unionor first_fourin CN_mobile:#同上

? ? ? ? ? ? ? ? print("China Telecom")

????????????????print("We're sending verification code via test to your phone:" + string)

????????????????break

? ? ? ? elif first_threein CN_telecomor first_fourin CN_telecom:#同上上

? ? ? ? ? ? ? ? print("China Telecom")

????????????????print("We're sending verification code via test to your phone:" + string)

????????????????break

? ? ? ? else:

????????????????print("Not such operator")

????else:

? ? ? ? print("Invalid length,your number should be in 11 digits")


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容