-1

Basically my plan was to return text with random-sized letters in words i.e. "upper" or "lower". The script is working, though it seems raw (I am a Beginner and I'd appreciate some corrections from You).

The problem is:

  1. It is not consistent. With that said, it can print word 'about' even if it should be 'About' or something similar.

  2. I want to be sure that the maximum of UPPER or lower letters in a row do not exceed 3 letters. and I don't know how to do it.

Thank you in advance.

#!/usr/bin/env python3
import random

message = input()
stop = ''

def mocking(message):
    result = ''
    for word in message:
        for letter in word:
                word = random.choice(random.choice(letter.upper()) + random.choice(letter.lower()))
                result += word
    return result



while stop != 'n':
    print(mocking(message))
    stop = input("Wanna more? y/n ").lower()
    if stop == 'n':
        break
    else:
        message = input()
gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45
  • You want at most 3 modifications ? – azro Oct 10 '20 at 20:54
  • You want "random" but "it is not consistent". And you want no more than three letters of the same case in a row. Are you sure that you want [random](https://en.wikipedia.org/wiki/Statistical_randomness), like flipping a coin to determine the case of the letters? – HABO Oct 11 '20 at 17:26

2 Answers2

0

You need to split the input into words, decide how many positions inside the word you want to change (minimum 3 or less if the word is shorter).

Then generate 3 unique positions inside the word (via random.sample) to change, check if upper then make lower else make upper. Add to resultlist and join words back together.

import random

message = "Some text to randomize"


def mocking(message):
    result = []
    for word in message.split():
        len_word = len(word)
        # get max 3 random positions
        p = random.sample(range(len_word),k = min(len_word,3))
        for position in p:
            l = word[position]
            if l.isupper():
                word = word[:position] + l.lower() + word[position+1:]
            else:
                word = word[:position] + l.upper() + word[position+1:]
               
        result.append(word)
        
    return ' '.join(result)

 
while True:
    print(mocking(message))
    stop = input("Wanna more? y/n ").lower()
    if stop == 'n':
        break
    else:
        message = input()

See Understanding slice notation for slicing

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • What if I wanted to preserve next letter in word to be lower() or upper() which depends on previous letters? I.e. if two-three words in a row come as upper, script makes next letter lower and vice versa? – PreacherBaby Oct 11 '20 at 12:14
  • @Preacher you think about how you could code it, try to code it, google if something similar is online, then ask a new question providing what you coded as [mre]. – Patrick Artner Oct 11 '20 at 17:20
0

At most 3 modifications? I would go with something like this.

def mocking(message):
    result = ''
    randomCount = 0
    for word in message:
        for letter in word:
            newLetter = random.choice( letter.upper() + letter.lower() )
            if randomCount < 3 and newLetter != letter:
                randomCount += 1
                result += newLetter
            else:
                result += letter
                randomCount = 0
    return result

If the random choice has modified the letter then count it.

  • I tried yours, but it still has some inconstistency. Your code looks much better though, I didn't even think about NOT putting everything under random.choice method instead of putting it under one. My problem is that I didn't even think to initialize a randomcount, what is it for and how does it apply? – PreacherBaby Oct 11 '20 at 11:52
  • If the random choice has modified the letter (made it different from the input) then the count is increased. When it reaches 3 modifications (2) it resets back to 0. Can you give an example of the inconsistencies? – Neil Reader Oct 11 '20 at 12:09
  • it happens often when you get to input a long text. Some words come in without being "mocked". Any word. – PreacherBaby Oct 12 '20 at 19:24