所以我正在尝试学习python,并且我编写了这段代码,它应该从随机单词和随机数列表中生成句子,我已经开始使用这段代码,但我不确定它是否会起作用

这是我的代码:

import random

num = random.randrange(0, 9)
drug = ['Advil', 'Synthroid', 'Crestor', 'Nexium', 'Vyvanse', 'Lyrica']
form = ['capsule', 'tablet', 'Powder', 'gel', 'liquid solution', 'Eye drops']

lines = []
for item in drug, form:
    line = '- the patient was prescribed [' + num + '](dosage) [' + item.form + '](form) of [' + item.drug + '] for [' + num + 'days](Duration)
    lines.append(line)

这是我期待的结果:

[the patient was prescribed [1](Dosage) [capsule](Form) of [Advil](Drug) for [5 days](Duration),
the patient was prescribed [2](Dosage) [Powder](Form) of [Nexium](Drug) for [6 days](Duration),
the patient was prescribed [5](Dosage) [luiquid solution](Form) of [Vyvanse](Drug) for [4 days](Duration),
...]
分析解答

这就是你如何做到的

import random

## generate 6 random numbers
nums = [random.randrange(0, 9) for _ in range(6)] 
days = nums = [random.randrange(0, 9) for _ in range(6)]
drugs = ['Advil', 'Synthroid', 'Crestor', 'Nexium', 'Vyvanse', 'Lyrica']
forms = ['capsule', 'tablet', 'Powder', 'gel', 'liquid solution', 'Eye drops']

lines = []
## zip will take one element from each array at one time
for num, drug, form, day in zip(nums, drugs, forms, days):
    ## this is string formatting syntax in python you put your local 
    ## variable inside curly brackets
    line = F"- the patient was prescribed {num} dosage {form} of {drug} for {day} days"
    lines.append(line)

print(lines)