Preface
I've started a new column, which is my personal diary and insights on learning Python, along with practice assignments.
What I'm writing today, the story of the outlaw, was a practice assignment from my first day of learning Python. The original assignment was to write a program for adding and removing names from a guest list, but on a whim, I wanted to try something harder than the practice assignment. Since my brother taught me how to use random for generating random content, I just went with it and wrote this.
The Story
There's a banquet tomorrow, and I want to invite some people to attend. The people I planned to invite were the outlaw Zhang San, Old Wang, Old Li, Old Guo, Old Meng, Little Wang, Little Li, Little Guo, Little Meng, Li Si, Zhang Yi, and Zhang Er. Because Zhang Yi and Zhang Er got into a fight, they can't make it. So I can invite these 10 people: the outlaw Zhang San, Old Wang, Old Li, Old Guo, Old Meng, Little Wang, Little Li, Little Guo, Little Meng, and Li Si. Even though I'd love to invite them all, our table can only seat 6.0 people, so I have to randomly pick 6.0 people without repeats. I told this to the outlaw Zhang San, and he said that Li Si absolutely must come, or else he'd show me what it really means to be an outlaw. So, next I have to pick 5 people, and I must also add Li Si to the mix. After 4 hours, I finally finished the random selection. I drew Old Meng, Little Li, Old Guo, Old Li, and Old Wang—these 5 people—and added Li Si. I went up to the outlaw Zhang San and told him the result, and he left satisfied. The next day, the 5 people I invited—Old Meng, Little Li, Old Guo, Old Li, and Old Wang—all showed up at the banquet. The outlaw Zhang San, having killed Li Si and gained EXP, attended the banquet along with these 6.0 people: Old Meng, Little Li, Old Guo, Old Li, and Old Wang. The outlaw Zhang San sat in the best seat and announced his grandest plan—to kill Luo Xiang! The room erupted in shock, but in the end, everyone agreed to the outlaw Zhang San's plan, and they began to carry it out step by step...
Code
# Import the random module
import random
# Assign values to X, Y, Z
x, y, z = 2, 4, 10
# How many people our round table can seat
People_sitting_on_the_table = (x*z+y)/4
# How many people can sit besides Li Si
People_sitting_on_the_table_2 = x*z-15
# List of people who can be invited
name_list = ['张一', '张二', '法外狂徒-张三', '老王', '老李', '老郭', '老孟', '小王', '小李', '小郭', '小孟']
# The Zhang Yi and Zhang Er problem
fight = '张一'
fight_2 = '张二'
name_list.remove('张一')
name_list.remove('张二')
# But the outlaw Zhang San invited Li Si to join the banquet
name_list.append('李四')
# Number of people in name_list right now
name_list_numbers = len(name_list)
# Output
print('明天有个宴会,我要邀请一些人来参加\n'
f"计划被邀请的人有{name_list[0]}、{name_list[1]}、{name_list[2]}、{name_list[3]}、{name_list[4]}、{name_list[5]}、{name_list[6]}、{name_list[7]}、{name_list[8]}、{name_list[9]}、"
f"{fight}、{fight_2}\n"
f"因为{fight}和{fight_2}在打架,所以来不了了。\n"
f"因此我可以邀请{name_list[0]}、{name_list[1]}、{name_list[2]}、{name_list[3]}、{name_list[4]}、{name_list[5]}、{name_list[6]}、{name_list[7]}、{name_list[8]}、{name_list[9]}"
f"这{name_list_numbers}个人。\n虽然我很想把他们都邀请来,但是我们的桌子只能坐下{People_sitting_on_the_table}个人,所以我必须随机抽{People_sitting_on_the_table}个人,并且不重复。\n"
f"我把这个消息说给了{name_list[0]},他说无论如何{name_list[-1]}必须来,否则他就要告诉我什么是法外狂徒\n"
f"因此,接下来我必须抽{People_sitting_on_the_table_2}个人,并且必须把{name_list[-1]}也加入进去")
# Technical limitation, so I can only do a draw-with-replacement story here
attend_name = random.choice(name_list)
attend_name_1 = random.choice(name_list)
attend_name_2 = random.choice(name_list)
attend_name_3 = random.choice(name_list)
attend_name_4 = random.choice(name_list)
# Second output
print('经过了4个小时终于完成了随机抽取。\n'
f"我抽出了,{attend_name}、{attend_name_1}、{attend_name_2}、{attend_name_3}、{attend_name_4}这{People_sitting_on_the_table_2}个人,并且加入了{name_list[-1]}。\n"
f"我走到法外狂徒-张三面前把,结果告诉了他,他心满意足的离开了。\n"
f"第二天,我邀请的{attend_name}、{attend_name_1}、{attend_name_2}、{attend_name_3}、{attend_name_4}这{People_sitting_on_the_table_2}个人都参加了这个宴席"
f"{name_list[0]}由于法外狂徒击杀了{name_list[-1]},增加了EXP"
f"因此和{attend_name}、{attend_name_1}、{attend_name_2}、{attend_name_3}、{attend_name_4}这{People_sitting_on_the_table}个人一起出席了这个宴席\n"
f'{name_list[0]}坐在最好的位置,说出了他最宏伟的计划——击杀罗翔!\n'
'室内一片哗然,但最终大家都同意了法外狂徒-张三的计划,并且一步一步实行着……')
Conclusion
On my first day of learning Python, I still can't get the random output to avoid repeats, so to get a random set of unique names, I have to run the program multiple times.