Exercise 4: Secret Code Language | Python Tutorial - Day #40

preview_player
Показать описание
Python is one of the most demanded programming languages in the job market. Surprisingly, it is equally easy to learn and master Python. This python tutorial for absolute beginners in Hindi series will focus on teaching you python concepts from the ground up.

python, C, C++, Java, JavaScript and Other Cheatsheets [++]:

►Learn in One Video[++]:

►Complete course [playlist]:

Follow Me On Social Media
Comment "#HarryBhai" if you read this 😉😉
Рекомендации по теме
Комментарии
Автор

agar abhi bhi ap ye course dekh rahe ho to you are legend bro salute to your consistency or ha ye @harrybhai ko koi taunt nahi hai . Course bohot hi accha hai bro keep going and keep growing

AmiteshVashishth
Автор

As a beginner I tried: I tried to use mostly all the logics learnt till now in your course.

import string
import random

def encode(normal_lang):
'''Converts your code to secret code'''
if (len(normal_lang)<3):
print(normal_lang[::-1])
elif (len(normal_lang)>3):
letter =
word_end = "".join(random.choices(string.ascii_lowercase, k=3))
word_begin = "".join(random.choices(string.ascii_lowercase, k=3))
secret_code = word_begin + letter + word_end
print(f"Your secret code is: {secret_code}")

def decode(normal_lang):
'''Converts your secret code to code'''
if (len(normal_lang)<3):
print(normal_lang[::-1])
elif (len(normal_lang)>3):
word = normal_lang[3:-3]
letter = word[-1]+word[:-1]
print(f"Your code is {letter}")

print("Hey! Do you want to encode or decode?")
choice = int(input("1 for encode and 2 for decode: "))

match choice:
case 1:
normal_lang = input("Enter you word to convert: ")
encode(normal_lang)
case 2:
normal_lang = input("Enter you word to convert: ")
decode(normal_lang)

sayanisen
Автор

Doing this exercise was so much fun. i tested my code with multiple edge cases and it was always fine. it can encode/decode full sentences including numbers, special characters, tabs, spaces, etc. (not the multiple lines tho)
Thank you so much harry sir for the python course and logic building.

here is my code:

import random
import string

def reverse(s):
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
return reversed_s

def get_random():
rand=""
for i in range(3):
rand =
return rand

def code(a):
if(len(a)<3):
a=reverse(a)
else:

return a

def decode(a):
if(len(a)<3):
a=reverse(a)
else:
a=a[3:-3]
a=a[-1]+a[0:-1]
return a

def codStr(a):
b=a
coded=""
while True:
j=b.find(" ")
str=b[:j]
coded=coded+code(str)+" "
b=b[j+1:]

if(b.find(" ")==-1):
coded=coded+code(b)
break
return coded

def decodStr(a):
b=a
decoded=""
while True:
j=b.find(" ")
str=b[:j]
decoded=decoded+decode(str)+" "
b=b[j+1:]

if(b.find(" ")==-1):
decoded=decoded+decode(b)
break
return decoded


while True:
a=input('enter your message: ')
b=int(input("enter 1 to lock, 2 to unlock: "))
if (b==1):
print("locked message: ", codStr(a))
elif (b==2):
print("unlocked message: ", decodStr(a))
else:
print("invalid. exiting")
break

jashan_iitroorkee
Автор

Here's My take: It uses random library so as the make the characters completely random, and also raises custom errors if the input isn't what I need!

import random

def Encrypt(word):

if len(word) <= 2:
return word[::-1]

else:
obj = word[1:]
obj = obj + word[0]
set = ""

for i in range(0, 3):
click = random.randrange(0, 2)
if click==0:
set = set+chr(random.randrange(97, 122))
else:
set = set+chr(random.randrange(65, 90))

obj = set+obj

set2 = ""

for j in range(0, 3):
click = random.randrange(0, 2)
if click==0:
set2 = set2+chr(random.randrange(97, 122))
else:
set2 = set2+chr(random.randrange(65, 90))

obj = obj+set

return obj


def Decrypt(word):
obj2=""
ans=""
if len(word) <= 2:
return word[::-1]

else:
obj2 = word[3:len(word)-3]
ans = obj2[len(obj2)-1] + obj2[0:len(obj2)-1]

return ans


try:
Choice = int(input("DO YOU WANT TO ENCRYPT(1) OR DECRYPT(2) A MESSAGE?: "))
if (Choice!=1 and Choice!=2):
raise IndexError

except ValueError:
print("INCORRECT VALUE ENTERED, YOU IMBECILE!")
except IndexError:
print("ENTER A VALUE 0 OR 1, IDIOT!")


if Choice == 1:
key = input("ENTER KEY: ")

if key == "RevUp":
Message = input("ENTER YOUR MESSAGE: ")
else:
print("GUESS YOU DON'T HAVE ACCESS!")

words = Message.split()
final = ""

for i in range(len(words)):
final = final + Encrypt(words[i]) + " "

print(final)

elif Choice == 2:
key = input("ENTER KEY: ")

if key == "RevUp":
Message = input("ENTER YOUR MESSAGE: ")
else:
print("GUESS YOU DON'T HAVE ACCESS!")

words = Message.split()
final = ""

for i in range(len(words)):
final = final + Decrypt(words[i]) + " "

print(final)

shreypatel
Автор

it has been an incredible experience. I have learned an impressive amount in such a short period and am excited to see how much more I will know as I continue. Based on my experience, this course and this channel are the best coding resources on YouTube.

shubhamkapure
Автор

for encoding
a=input("enter a string")
if len(a)<=2:
a=a[::-1]
print(a)
elif len(a)>2:
add1=input("enter 3 alphabets to the start")
add2=input("enter 3 alphabets to the end")
modified=a[1:]+a[0]
add=add1+modified+add2
print(add)

For decoding
b=input("enter the word to decode")
if len(b)<=2:
b=b[::-1]
print(b)
elif len(b)>2:
modified1=b[3:-4]
modified=modified1
modified2=b[-4]
modified=modified2+modified1
print(modified)

mubasshiraquraishi
Автор

'''
Rules for Encoding -
1. If Word Length < or = 3 (that is, 2 acc. to index), take first letter and append it at the end
2. Else, Reverse the word

Rules for Decoding -
1. If Word Length < or = 3 (that is, 2 acc. to index), take last letter and put it on first
2. Else, Reverse the word
'''

print('Welcome to Code-Decode!')
x = int(input('Enter 1. for Code and 2. for Decode\n'))


def code(input):
if len(input) < 3 or len(input) == 3:
print(input[1::] + input[0])
else:
print(input[::-1])


def decode(input):
if len(input) < 3 or len(input) == 3:
print(input[-1] + input[:-1])
else:
print(input[::-1])

if x == 1:
code(input('Enter the word you want to encode - \n'))
elif x == 2:
decode(input('Enter the word you want to decode - \n'))
else:
print('Wrong statement! Choose either 1 or 2!')

neerajbeniwal
Автор

200star out of 5 star for this course. Thank you very much sir.

kavyagandhi
Автор

I am beginner and i write this code and i take little bit help of AI:

import random
a = input('Do you want to code or decode? ')
if a=='code':
print('Enter your message: ')
b = input()
if len(b)>3:
c = b[::-1]
ran = ['abc', 'rcf', 'ijf', 'iue', 'jir', 'ori']
ran1 = ['xyz', 'try', 'uri', 'eui', 'rty','jio']
start = random.choice(ran)
end = random.choice(ran1)
print('Your coded message is:', start + c + end)
else:
print('Your coded message is:', b[::-1])
elif a=='decode':
print('Enter your message: ')
c = input()
if len(c)>3:
d = c[3:-3]
print('Your decoded message is:', d[::-1])
else:
print('Your decoded message is:', c[::-1])
else:
print('Invalid Choice')

SultanMuhammadArslan
Автор

This is the best course for beginners to learn python in simplest
love you harry bhai..

creativeknowledge
Автор

10/10.... I am Solution Architect with over 11 Y of Experience. Still i feel, this course is kick start of python coding and worth accepting 100 Days Challenge. 99.99% people will feel complete post attending this course. Good Work Harry!. I feel, you are carrying mission the way "KHAN SIR GS PATNA " is doing.. Lucky to find your course on YouTube, which made me motivated to peruse my interest toward python. "EDUCATION IS FOR ALL, AN EDUCATED PERSON CAN ONLY UNDERSTAND THIS...Way to go.."

AMARJEETKUMAR-cesc
Автор

Exercise Answer:
choice = input("To Code Message Press 1 To Decode Message Press 2: ")
if(choice == "1"):
msg = input("Enter The Message To Be Coded: ")
if(len(msg) > 3):
encode1 = msg[1:] + msg[0]
encode2 = msg[2:5] + encode1 + msg[0:3]
print(encode2)

else:
print(msg[::-1])


elif(choice == "2"):
msg = input("Enter The Code: ")
if(len(msg) >3):
step1 = msg[:-3]
step2 = step1[3:]
step3 =step2[-1] + step2[:-1]
print(step3)

else:
print(msg[::-1])

else:
print("Invalid Operation")

atharvamrutkar
Автор

a=(input("Enter the word: "))
if(len(a)>=3):
b=a[1:]+a[-4]
modified=input("Enter the random character for starting: ")
modified2=input("Enter the random character for ending: ")
c=modified+b+modified2
print(c)
else:
print(a[::-1])
x=input("Enter the decode: ")
if(len(x)>=3):
y=x[3:-4]
z=x[-4]
b=z+y
print(b)
else:
print(x[::-1])

zaid
Автор

easiest of them all
x=int(input("enter 1: for encoding and 2 for decodinng "))
if x==1:
print ("encoded case")
a=input("enter the string ")
if len(a)<3:
c=a[::-1]
print(c)
elif len(a)>=3:
b=a[1:]+a[0]
print (b)
sd=input("enter three random characters ")
bd=input("enter three random characters ")
ee=sd+b+bd
print(ee)
elif x==2:
print("decoding case")
b=input("enter the string ")
if len(b)<3:
er=b[::-1]
else:
ee=b[3:-4]+b[-4]
we=ee[::-1]
print(we)

pranavdath
Автор

This is the most easiest way to do it I think :
x=int(input("press 1 for ENCODE or anyother number for DECODE :"))
if x == 1: #Coding
str = input("\nEnter the word to Code :")
l = list(str)
if len(str) >= 3:
l2 = l[0]
l = l[1:]
l += l2
rand = ['a', 'c', 'z']
l += rand
l[:0] = rand
print("Output :", ''.join(l), )
else:
l.reverse()
print("Output :", ''.join(l), )

#Decoding
else:
str = input("\nEnter the word to Decode :")
l = list(str)
if len(str) <= 3:
l.reverse()
print("Output :", ''.join(l), )
else:
l = l[3:-3]
l2 = l.pop()
l[:0] += l2
print("Output :", ''.join(l), )

strangeyt
Автор

a = You are best teacher of programming i am in class 8 and i am understanding this very easily
b = thank you
print (a)
print (b)

mayankgaming
Автор

Hello Harry sir, I just wrote a code for the exercise of encode and decode.
userinput=str(input("enter the word which you want to encode or decode:"))
result=int(input("enter 1 for encode and 2 for decode"))
if(result==1):
if(len(userinput)<3):
x=userinput[::-1]
print(x)
else:
x= 'lkg'
y='per'
z=userinput[0:1]
a=userinput[1:]
print(x+a+z+y)
elif(result==2):
if(len(userinput)<3):
x=userinput[::-1]
print(x)
else:
z=userinput[-4:-3]
a=userinput[3:-4]
print(z+a)
else:
print("enter the valid encode and decode option")

saritakaita
Автор

🔥🔥🔥 I have watch c language full tutorial course

sawaijangid
Автор

k=str(input("Enter any word:") )
if len(k)>3:
fc=k[0]
k=k[1:]
k=k+fc
s="idk"
e="idk"
k=s+k+e
print(k)
else:
reverse=k[::-1]
print(reverse)

if len(k)<3:
reverse=k[::-1]
print(reverse)
else:
lc=k[-4]
k=k[3:-3]
k=lc+k
k=k[0:-1]
print(k)

emily_core.
Автор

Great Vid! but the repl link in description is to day 38 wheres day 40 where we can post our code?

kartikeykakaria