import random
user = "Jude"
file = ["do not lie", "do not cheat", "do not be bitter in anger", "do love all, is the greatest commandment"]
task = (input(f"There are four task in the task menu nHow many task do wish to complete today {user}: "))
options1, options2, options3, options4 = file
for task in [random.randrange(*sorted([options1, options2, options3, options4])) for i in range(3)]:
while True:
if task == "1":
print(options1)
break
elif task == "2":
print(options1)
print(options2)
break
elif task == "3":
print(options1)
print(options2)
print(options3)
break
elif task == "4":
print(options1)
print(options2)
print(options3)
print(options4)
break
else:
print("No task to be done today")
break
我正在尝试编写的程序预计将打印用户输入的任务数。例如,如果用户输入2个任务,预计程序将为用户随机选择两个任务并打印出来。我无法使程序正常工作,它一直给出这个错误;
"C:UsersJude EPycharmProjectspythonProject3venvScriptspython.exe" D:/python_tutorial/todolist.py
There are four task in the task menu How many task do wish to complete today Jude: 2 Traceback (most recent call last): File "D:python_tutorialtodolist.py", line 7, in <module>
for task in [random.randrange(*sorted([options1, options2, options3, options4])) for i in range(3)]: File "D:python_tutorialtodolist.py", line 7, in <listcomp>
for task in [random.randrange(*sorted([options1, options2, options3, options4])) for i in range(3)]: TypeError: randrange() takes from 2 to 4 positional arguments but 5 were given
请问我如何修复它,或者我如何让这个程序工作?
谢谢。
random.randrange
从两个整数中随机选择一个数字。你想要random.choice
,它从列表中选择一个元素。如果你想要不止一个,你可以使用random.choices
。如果您希望避免重复,请使用random.sample