Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make this program in python?

I'm try to make this: your enter a word like this: Happy and than the program returns somethings like : yppaH or appHy.

The problem is that I get just one letter : y or H, etc..

import random
def myfunction():
    """change letter's position"""
    words = input("writte one word of your choice? : ")
    words = random.choice(words)
    print('E-G says : '+ words)
like image 473
Georges Kayembe Avatar asked Sep 06 '26 06:09

Georges Kayembe


1 Answers

You have to use sample, not choice.

import random
# it is better to have imports at the beginning of your file
def myfunction():
    """change letter's position"""
    word = input("writte one word of your choice? : ")
    new_letters = random.sample(word, len(word))
    # random.sample make a random sample (without returns)
    # we use len(word) as length of the sample
    # so effectively obtain shuffled letters
    # new_letters is a list, so we have to use "".join
    print('E-G says : '+ "".join(new_letters))
like image 96
Ilya V. Schurov Avatar answered Sep 07 '26 20:09

Ilya V. Schurov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!