Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

python - Order generator program

I am trying to make a short program which will take user input, append the input to a list and then randomly determine the order of the elements in the list.

this is what I thought out:

from random import choice
 
participants = []
 
prompt = "
Please enter players first name: "
prompt += "
Please enter 'q' when all players have been entered: "
 
while True:
    user = input(prompt)
    if user == 'q':
        break
        print(f"There are {slot} participants")

    else:
        participants.append(user)
        slot = len(participants)
        print("

")
        for participant in participants:
            print(f"{participant}")
        print(f"So far, there are {slot} participants")

while participants:
    for participant in participants:
        person_of_choice = choice(participants)
        person_in_question = participants.remove(person_of_choice)
        print(person_in_question)

However, I am getting the following output

Mark Stark Dark So far, there are 3 participants

Please enter players first name: Please enter 'q' when all players have been entered: q None None None

How do I change the ordering from none into their names?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've changed a few things in the code, and the changes are commented out with ##. you can try it to see the output.

from random import choice

participants = []

prompt = "
Please enter players first name: "
prompt += "
Please enter 'q' when all players have been entered: "
# init slot to zero
slot = 0
while True:
    user = input(prompt)
    if user == 'q':

        print(f"There are {slot} participants")
        ## if you want to use `print` function before `break`,
        ## you must put the `break` statement under the `print` function
        break
    else:
        participants.append(user)
        slot = len(participants)
        print("

")
        for participant in participants:
            print(f"{participant}")
        print(f"So far, there are {slot} participants")

while participants:
    for participant in participants:
        person_of_choice = choice(participants)
        print(person_of_choice)
        # person_in_question = participants.remove(person_of_choice)
        ## person_in_question is None, because `remove` always return None
        ## you can this from here https://docs.python.org/3/tutorial/datastructures.html
        participants.remove(person_of_choice)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...