D S*_*nas 1 python random list
我们需要创建一个可用于"秘密圣诞老人"游戏的程序:
from random import *
people=[]
while True:
person=input("Enter a person participating.(end to exit):\n")
if person=="end": break
people.append(person)
shuffle(people)
for i in range(len(people)//2):
print(people[0],"buys for",people[1])
Run Code Online (Sandbox Code Playgroud)
这是我开发的程序.截至目前,如果我输入3个人(例如Bob,Ben,Bill),它将返回"Ben购买Bill",没有人购买Ben或Bob.我目前正试图让它输出"Bob买Ben,Ben Buys买Bill,Bill买Bob",但到目前为止还没有成功.如果有人能给我一个暗示/基础来设置它,将不胜感激.另外,如果我的代码中有任何不允许我完成此操作的错误,您能告诉我吗?谢谢.
第一个提示,使用0和1之类的常量而不是i你的内部是没有意义的for循环.
for i in range(len(people)):
print(people[i],"buys for",people[(i+1)%(len(people))])
Run Code Online (Sandbox Code Playgroud)
但是,这种实施方式不会为您提供秘密圣诞老人提供的所有可能性.
假设您输入"Alice","Bob","Claire","David",您将永远不会遇到以下情况:
您只能获得循环排列,即:
和我一样.
你需要一些额外的工作才能做出完美的秘密圣诞老人:)
| 归档时间: |
|
| 查看次数: |
4878 次 |
| 最近记录: |