#Python 初學之二編寫一個小遊戲#
阿新 • • 發佈:2019-02-16
#密碼輸入三次錯誤輸入限制
password_list = ['####','12345']
def account_login():
cishu = 3;
while cishu >0:
password = input ('password:');
password_correct = password ==password_list[-1];
password_reset = password == password_list[0];
if password_correct:
print('login right!');
elif password_reset:
new_password = input('Enter a new password:');
password_list.append(new_password);
print('your password has changed successfully!' );
account_login();
else:
print('wrong login');
cishu = cishu-1;
print(cishu,'times left');
else:
print('Your account has ben suspended');
account_login();
output
password:34343434
wrong login
2 times left
password:34334
wrong login
1 times left
password:34334
wrong login
0 times left
Your account has ben suspended
Process finished with exit code 0
#Python 編寫一個小遊戲!
import random;
def roll_dice(numbers=3,points=None): #隨機的三個數
print('<<<<<<<<<<<<<<<<ROLL THE DICE!>>>>>>>>>>>>>>>');
if points is None:
points = [];
while numbers >0:
point = random.randrange(1,7);
points.append(point);
numbers-=1;
return points;
def roll_result(total):
isBig = 11<= total <= 18;
isSmall = 3 <= total <= 10;
if isBig:
return 'Big';
elif isSmall:
return 'Small';
def start_gamw():
print('<<<<<<<<<<<<<<<<GAME STARTS!>>>>>>>>>>>>>>>>>');
choices = ['Big','Small'];
your_choices = input('Big or Small:');
if your_choices in choices:
points = roll_dice();
total = sum(points);
youWin = your_choices == roll_result(total);
if youWin:
print('The points are',points,'You win');
start_gamw();
else:
print('The points are',points,'You lose!');
start_gamw();
else:
print('Invalid Words');
start_gamw();
start_gamw();