Python daily question: exercise the use of Python grammar, exercise thinking and logic, and cultivate algorithm skills.
topic:
The user randomly enters the serial number corresponding to rock, paper, scissors, and when the input is empty, the game ends; the computer randomly generates a random number between 0 and 2, and then follows:
The rules of the game of rock-scissors, paper-scissors, and rock -playing, judge the win or loss between the computer and the user, and record the number of completions.
analysis:
1. Use the list to store the three elements of rock, scissors, and cloth for later display;
2. Print the serial number prompting the rock, paper, scissors, and let the user input;
3. The computer generates a random value, and judges the win or lose of the game according to the random value and the rules of the game;
4. Create variable records, the number of games and the number of player wins.
achieve:
import random
def rock_paper_scissors_game():
game_list = ['石头', '剪刀', '布']
victory_count = 0 # 胜利次数
failure_count = 0 # 失败次数
draw_count = 0 # 平局次数
game_count = 1 # 记录游戏次数
while True:
computer_num = random.randint(0, 2)
print('------------ 第{}局 ------------'.format(game_count))
for i, v in enumerate(game_list):
print('{}、{}\t\t'.format(i, v), end='')
print('\n------------------------------')
user_num = input('请输入【0-2】之间的整数:')
if not user_num:
print('您的战绩是:【{}】胜【{}】负【{}】平'.format(victory_count, failure_count, draw_count))
break
user_num = int(user_num)
if 0 <= user_num < 3:
if user_num == 0:
if computer_num == 0: # 用户出:石头
print('您出的是【{0}】,电脑出的是【{0}】 --> 平局'.format(game_list[user_num]))
draw_count += 1
elif computer_num == 1:
print('您出的是【{}】,电脑出的是【{}】 --> 您赢'.format(game_list[user_num], game_list[computer_num]))
victory_count += 1
else:
print('您出的是【{}】,电脑出的是【{}】 --> 电脑赢'.format(game_list[user_num], game_list[computer_num]))
failure_count += 1
elif user_num == 1: # 用户出:剪刀
if computer_num == 0:
print('您出的是【{}】,电脑出的是【{}】 --> 电脑赢'.format(game_list[user_num], game_list[computer_num]))
failure_count += 1
elif computer_num == 1:
print('您出的是【{0}】,电脑出的是【{0}】 --> 平局'.format(game_list[user_num]))
draw_count += 1
else:
print('您出的是【{}】,电脑出的是【{}】 --> 您赢'.format(game_list[user_num], game_list[computer_num]))
victory_count += 1
else: # 用户出:布
if computer_num == 0:
print('您出的是【{}】,电脑出的是【{}】 --> 您赢'.format(game_list[user_num], game_list[computer_num]))
victory_count += 1
elif computer_num == 1:
print('您出的是【{}】,电脑出的是【{}】 --> 电脑赢'.format(game_list[user_num], game_list[computer_num]))
failure_count += 1
else:
print('您出的是【{0}】,电脑出的是【{0}】 --> 平局'.format(game_list[user_num]))
draw_count += 1
game_count += 1
print('*' * 50)
else:
print('您的输入有误!')
print('*' * 50)
rock_paper_scissors_game()
result:
------------ 第1局 ------------
0、石头 1、剪刀 2、布
------------------------------
请输入【0-2】之间的整数:0
您出的是【石头】,电脑出的是【剪刀】 --> 您赢
**************************************************
------------ 第2局 ------------
0、石头 1、剪刀 2、布
------------------------------
请输入【0-2】之间的整数:1
您出的是【剪刀】,电脑出的是【布】 --> 您赢
**************************************************
------------ 第3局 ------------
0、石头 1、剪刀 2、布
------------------------------
请输入【0-2】之间的整数:0
您出的是【石头】,电脑出的是【布】 --> 电脑赢
**************************************************
------------ 第4局 ------------
0、石头 1、剪刀 2、布
------------------------------
请输入【0-2】之间的整数:
您的战绩是:【2】胜【1】负【0】平
[ Conclusion ] There are still many incomprehensions in the implementation logic of the entire program above, and you are welcome to give pointers; if you think the author is not easy, please give me a thumbs up and give me the motivation to record more articles! !