以下是一个简单的示例代码,可以用python编写取火柴的程序:
import randomdef take_match(num_matches):# 检查火柴数量是否大于零if num_matches <= 0:print("没有火柴可取了!")else:# 随机取火柴num_taken = random.randint(1, num_matches)print("取走了", num_taken, "根火柴。")num_matches -= num_takenprint("还剩下", num_matches, "根火柴。")# 测试num_matches = 10take_match(num_matches)这个程序使用了random模块来生成一个随机数来表示取多少根火柴。num_matches变量表示火柴的总数量,通过调用take_match函数来取火柴,函数会打印出取火柴的数量和剩余的火柴数量。

