Georges Kayembe
Georges Kayembe

Reputation: 121

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)

Upvotes: 2

Views: 80

Answers (3)

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

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

Upvotes: 6

Tomasz Plaskota
Tomasz Plaskota

Reputation: 1357

If you want to print reversed word this would be fastest approach:

print(input("writte one word of your choice? : ")[::-1])

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

Use random.shuffle on a conversion of the string in a list (works in-place)

Then convert back to string using str.join

import random

s =  "Happy"

sl = list(s)
random.shuffle(sl)

print("".join(sl))

outputs:

pyapH
Hpayp

Upvotes: 5

Related Questions