简体   繁体   English

以有趣的方式从列表中选择一个随机名称 (Python)

[英]Choosing a random name from a list in a fun way (Python)

So I would like pick a random name from a list and slowly reveal the winner, here it what I have so far:所以我想从列表中随机选择一个名字,然后慢慢地揭示获胜者,这是我目前所知道的:

import time
import random

names = ['Isaac', 'Matthew', 'Cameron', 'Andy', 'Rob']
blanks=''

winner =random.choice(names)
print(winner)

for i in range(len(winner)):
  blanks+= '_'

print(blanks)


from random import shuffle

def shuffle_word(word):
  word = list(word)
  shuffle(word)
  return ''.join(word)

scrambled = shuffle_word(winner)
print(scrambled)

I want the code to print the _ _ _ _ _ first and then reveal the missing letters of the scrambled name one by one using time.sleep() to make it more exciting.我希望代码先打印 _ _ _ _ _,然后使用 time.sleep() 逐一显示打乱名称中缺失的字母,以使其更精彩。 One the full scrambled name is revealed, I'd like to 'unscramble' the name and print the winner.一个完整的加扰名称被显示出来,我想“解读”这个名称并打印获胜者。

Could anyone help me do this?谁能帮我做这个?

Try the below snippet.试试下面的片段。 Add this at the last if your code and also understand the code so that it would be easier for you to Modify it if in case:如果您的代码并理解代码,请在最后添加此代码,以便您在万一情况下更容易修改它:

for i in range(0, len(scrambled)):
    print(scrambled[:i]+'_'*(len(scrambled)-i))
    time.sleep(3)
print(scrambled)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM