我试图建立一个抽奖系统,让你选择参与者,然后打印到屏幕上。我想让它与Python3和Python2交叉兼容。我对输入有问题。在要求您输入参与者姓名的地方输入的信息总是给我一个错误:
Traceback (most recent call last):
File "employee-raffle.py", line 20, in
participant_list.append(input("Enter person " + str(len(participant_list) + 1) + ": "))
File "", line 1, in
NameError: name 'test' is not defined
起始代码:# Import modules
import time
import random
print("Welcome to the raffle ")
participants = 0
# Checks how many people are participating in the raffle
while True:
try:
participants = int(input("\nHow many people are there in the raffle? \n"))
break
except:
print("Invalid option")
continue
# Creates a list and asks user to input the names of everyone
participant_list = []
while participants > len(participant_list):
participant_list.append(input("Enter person " + str(len(participant_list) + 1) + ": "))
# Chooses a random person
random_participant = participant_list[random.randint(0,len(participant_list ) - 1)]
# Prints the person to the screen
print("AND THE WINNER IS:")
print("3")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
time.sleep(1)
print(random_participant)
它在python3中似乎工作得很好。我真的希望它能同时使用,因为我正在努力学习如何使事物交叉兼容,因为在我看来,这是一个重要的编程实践。在