简体   繁体   中英

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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