Python Learning Diary – The Story of the Outlaw
Preface
I started a new column to document my personal Python learning diary, insights, and practice exercises.
The story of the outlaw I wrote today was a practice exercise from my first day of learning Python. The original assignment was to add and delete guest names from a list, but on a whim, I wanted to try something harder than the exercise. My brother taught me how to use random to generate random content, so I just wrote something casually.
Story
Tomorrow there's a banquet, and I want to invite some people. The planned invitees are the outlaws – 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 are fighting, they can't come. So I can invite the outlaws – Zhang San, Old Wang, Old Li, Old Guo, Old Meng, Little Wang, Little Li, Little Guo, Little Meng, and Li Si – 10 people in total. Although I really want to invite all of them, our table can only seat 6.0 people, so I have to randomly pick 6.0 people without repetition. I told this to the outlaw – Zhang San, and he said that Li Si must come no matter what, otherwise he would show me what it means to be an outlaw. Therefore, I now have to pick 5 people and must include Li Si. After 4 hours, I finally completed the random selection. I picked Old Meng, Little Li, Old Guo, Old Li, and Old Wang – these 5 people – and added Li Si. I went to the outlaw – Zhang San and told him the result. He left satisfied. The next day, the 5 people I invited – Old Meng, Little Li, Old Guo, Old Li, and Old Wang – all attended the banquet. The outlaw – Zhang San, because the outlaw killed Li Si and gained EXP, attended the banquet together 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 was in an uproar, but in the end, everyone agreed to the outlaw – Zhang San's plan and began to execute it step by step…
Code
# Import the random module
import random
# Assign values to X, Y, Z
x, y, z = 2, 4, 10
# Number of people our round table can seat
People_sitting_on_the_table = (x*z+y)/4
# Number of people that can sit besides Li Si
People_sitting_on_the_table_2 = x*z-15
# List of invitees
name_list = ['Zhang Yi', 'Zhang Er', 'Outlaw-Zhang San', 'Old Wang', 'Old Li', 'Old Guo', 'Old Meng', 'Little Wang', 'Little Li', 'Little Guo', 'Little Meng']
# Zhang Yi and Zhang Er's problem
fight = 'Zhang Yi'
fight_2 = 'Zhang Er'
name_list.remove('Zhang Yi')
name_list.remove('Zhang Er')
# But the outlaw-Zhang San invited Li Si to join the banquet
name_list.append('Li Si')
# Number of people in name_list now
name_list_numbers = len(name_list)
# Output
print('Tomorrow there is a banquet, I want to invite some people to attend\n'
f"The planned invitees are {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"Because {fight} and {fight_2} are fighting, they can't come.\n"
f"So I can invite {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"these {name_list_numbers} people.\nAlthough I really want to invite all of them, our table can only seat {People_sitting_on_the_table} people, so I have to randomly pick {People_sitting_on_the_table} people without repetition.\n"
f"I told this to {name_list[0]}, and he said that {name_list[-1]} must come no matter what, otherwise he would show me what it means to be an outlaw\n"
f"Therefore, I now have to pick {People_sitting_on_the_table_2} people and must include {name_list[-1]}")
# Technical limitation, can only make a story of drawing balls with replacement
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('After 4 hours, I finally completed the random selection.\n'
f"I picked {attend_name}, {attend_name_1}, {attend_name_2}, {attend_name_3}, {attend_name_4} these {People_sitting_on_the_table_2} people, and added {name_list[-1]}.\n"
f"I went to the outlaw-Zhang San and told him the result. He left satisfied.\n"
f"The next day, the {People_sitting_on_the_table_2} people I invited – {attend_name}, {attend_name_1}, {attend_name_2}, {attend_name_3}, {attend_name_4} – all attended the banquet"
f"{name_list[0]} because the outlaw killed {name_list[-1]} and gained EXP"
f"therefore attended the banquet together with {attend_name}, {attend_name_1}, {attend_name_2}, {attend_name_3}, {attend_name_4} these {People_sitting_on_the_table} people\n"
f'{name_list[0]} sat in the best seat and announced his grandest plan – to kill Luo Xiang!\n'
'The room was in an uproar, but in the end, everyone agreed to the outlaw-Zhang San\'s plan and began to execute it step by step…')
Conclusion
On the first day of learning Python, I still couldn't output random values without repetition, so the names appearing require multiple runs to get random, non-repeating values.