Python Forum
American Adolescent language in python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
American Adolescent language in python.
#1
Basically I have to write a python program to translate the english sentence entered by the user into the "American Adolescent" language by inserting "you know" after every 3 word.

below is basically what I got so far... I know i have to loop through it and increment a counter by one each time someone hit a space but I don't know how to do it in python.

I attached what I have so far.
buran write Feb-18-2022, 10:24 AM:
Please, don't include images of code, error, data, etc. Copy/paste as text and format using BBCode tags.

Attached Files

Thumbnail(s)
   
Reply
#2
You can copy and paste you code using the "Insert python" button so that it posts like this. It will make it much easier for someone to give you an appropriate answer. You may want to use a counter variable to know when you're at every third word.
user_str = input ("Please type an english sentence: ")
str1 = user_str
str2 = ""
index = 0

while index < len (str1) :
	if str1 [index] == chr (32) :
		str2 = str2 + " you know "
		index += 1
	else :
		str2 = str2 + str1 [index]
		index += 1

print (str2)

Attached Files
Image(s)
   
Reply
#3
There are always simple way and the other way. Code copy-paste is simple way and messing with pictures is the other. This just makes harder for other people to help.

Now to the problem: inserting "you know" after every 3 word.

Where should we start? Maybe from splitting text into words? As we don't need list as end result, we just generate stream of words:

s = "There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory mentioned, which states that this has already happened"

print(*(word for word in s.split()))

-->There is a theory /.../ has already happened
It's looks like original string printed but it's actually streamed and printed word by word. One can ask - so what? If we are able to stream words, can we find right spot (after every third word) to insert 'you know'? Yes, we can. We just count words as we would do on paper. For such counting Python has built-in enumerate():

print(*enumerate(word for word in s.split()))

--> (0, 'There') (1, 'is') (2, 'a') (3, 'theory') /.../ (45, 'has') (46, 'already') (47, 'happened')
How do we determine third word? It's divisible by 3 with no reminder (i % 3 == 0). In order this to work we must start enumerating from 1 instead of default 0.

So we stream enumerated words, if its third word we add 'you know' else just word.

insertion = 'you know'
stream = enumerate((word for word in s.split()), start= 1)
print(*(f'{word} {insertion}' if i % 3 == 0 else word for i, word in stream))

--> There is a you know theory which states you know that if ever you know anyone discovers exactly 
you know what the Universe you know is for and you know why it is you know here, it will you know 
instantly disappear and you know be replaced by you know something even more you know bizarre 
and inexplicable. you know There is another you know theory mentioned, which you know states that 
this you know has already happened you know
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Translating language in python Liquid_Ocelot 11 10,231 Apr-02-2017, 01:36 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020