我正在开发一个程序来模拟Secret Santa的分类帽。我试图让程序有一个错误陷阱来阻止人们获取自己的名字,但是如果有人得到他们自己的名字,我就无法让程序选择一个新名字。我遇到的另一个问题是该程序过早地退出。
这是我的代码:
import random
print "Testing Arrays"
Names=[0,1,2,3,4]
#0 - Travis
#1 - Eric
#2 - Bob
#3 - Tim
#4 - Dhyan
x = 1
z = True
def pick(x):
while (z == True):
#test=input("Is your Name Travis?")
choice = random.choice(Names) #Picks a random choice from Names Array
if (choice == 0): #If it's Travis
test=input("Is your Name Travis?") #Asking user if they're Rabbit
if(test == "Yes"):
return "Pick Again"
elif(test== "No"):
return "You got Travis"
Names.remove(1)
break
elif (choice == 1):
test=input("Is your Name Eric?")
if(test=="Yes"):
return "Pick Again"
elif(test=="No"):
Names.remove(2)
return "You got Eric"
break
print pick(1)
答案 0 :(得分:1)
首先询问用户名,然后使用while循环继续获取随机名称,而随机名称等于输入名称。
答案 1 :(得分:1)
While this may not be exactly how you want to organize your program, the example provides an example of one way to prevent individuals from giving gifts to themselves. It uses something similar to a do/while loop available in some other languages to make sure _.each(items,function(item){_.set(item,'a',5)});
pass the requirements.
targets