Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 秘密圣诞老人分类帽_Python_Arrays_Function_Loops_Random - Fatal编程技术网

Python 秘密圣诞老人分类帽

Python 秘密圣诞老人分类帽,python,arrays,function,loops,random,Python,Arrays,Function,Loops,Random,我正在做一个程序,它将模拟秘密圣诞老人的分类帽。我试图让程序有一个错误陷阱,以防止人们获得自己的名字,但我无法让程序在有人获得自己的名字时选择一个新的名字。我遇到的另一个问题是,程序一直过早退出 这是我的密码: 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): wh

我正在做一个程序,它将模拟秘密圣诞老人的分类帽。我试图让程序有一个错误陷阱,以防止人们获得自己的名字,但我无法让程序在有人获得自己的名字时选择一个新的名字。我遇到的另一个问题是,程序一直过早退出

这是我的密码:

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)

首先询问用户名,然后使用while循环在random name等于input name时继续获取随机名称

虽然这可能不是您想要组织计划的确切方式,但本示例提供了一种防止个人向自己赠送礼物的方法。它使用类似于其他一些语言中可用的do/while循环的东西来确保
目标
通过要求

#! /usr/bin/env python3
import random


def main():
    names = 'Travis', 'Eric', 'Bob', 'Rose', 'Jessica', 'Anabel'
    while True:
        targets = random.sample(names, len(names))
        if not any(a == b for a, b in zip(targets, names)):
            break
    # If Python supported do/while loops, you might have written this:
    # do:
    #     targets = random.sample(names, len(names)
    # while any(a == b for a, b in zip(targets, names))
    for source, target in zip(names, targets):
        print('{} will give to {}.'.format(source, target))


if __name__ == '__main__':
    main()