Learn to Program 3 : Math Strings Exception Handling

by CHZ



Code & Transcript : http://goo.gl/NtyyAT
Get my Ultimate Python Tutorial Series for FREE until September 27 : http://bit.ly/UltimatePython

Best Python Book :http://amzn.to/28Ungsd

Support me on Patreon : https://www.patreon.com/derekbanas

In this part of my Learn to Program tutorial I will continue teaching how to program through solving programming problems. We will cover Exception Handling, Do While Loops, the Math Module, the Decimal Module, Strings, and a whole lot more.

Thank you to Patreon supports like the following for helping me make this video

https://www.facebook.com/cottageindustriesbuild/
@kyleaisho
@thetwistedhat
vjFaLk

source

34 comments

Dileep Reddy August 1, 2020 - 6:44 am

Hey Derek, I solved the secret message problem in a different way and I just wanted to share it with everyone

secret = input("Enter the secret message: ")

space1 = " "

dummy = ''

#Convert to unicodes

for i in secret:

space1 = space1 + str(ord(i)) + " "

dummy += str(ord(i))

code = space1[1:-1]

print("Secret message: ",dummy)

#Convert the secret message back to original

code_list = code.split(" ")

space2 = " "

for j in code_list:

convert = int(j)

space2 = space2 + chr(convert)

#Print the actual message

message = space2[1:]

print("Actual code is: ", message)

Reply
edi sakhnovich August 1, 2020 - 6:44 am

Do we have operator overloading in Python?

Reply
Abhinab k August 1, 2020 - 6:44 am

inp_string=str(input("Enter a string "))
def encode(n):
print("Secrete message is ")
for x in inp_string:
print(ord(x),end="")
print()

def decode(n):
print("Decoded message is ")
for x in inp_string:
y=ord(x)
print(chr(y),end="")

encode(inp_string)
decode(inp_string)

Reply
Abhinab k August 1, 2020 - 6:44 am

partially initialized module 'math' has no attribute 'ceil' (most likely due to a circular import)
what is this error?

Reply
Ayoub Bezia August 1, 2020 - 6:44 am

you're the best

Reply
Leonardo Angel Cárrega August 1, 2020 - 6:44 am

Your tutorials are awesome! Thanks!

Reply
H Stevens August 1, 2020 - 6:44 am

.floor = trunc

Reply
Alphabet August 1, 2020 - 6:44 am

<>

Reply
Vireakbodh Yay August 1, 2020 - 6:44 am

Here's how I did it. Pretty different from yours.
original_word = input("Enter an uppercase string to hide : ")
original_word = str(original_word)

print("Secret Message :", end="")
for i in range(0, len(original_word)):
print(ord(original_word[i]), end="")

print()
print("Original Message :", original_word, end="")

edit : didn't realize there was gonna be homework and bonus homework. To clarify this only serves the uppercases.

edit1 : to serve both upper and lower cases, just add -23 after print(ord(original_word[i])

Reply
Ihor K August 1, 2020 - 6:44 am

my_string = input("Input a string to hide in UPPERCASE : ")
sec_string = ""
original = ""

for i in range(0, len(my_string)):
sec_string += str(ord(my_string[i]))
s = ord(my_string[i])
original += chr(s)

print("Your secter string is:", sec_string)
print("Your original string is:", original)

Reply
Garrett S. White August 1, 2020 - 6:44 am

line 29, I thought char_code was already an int? or a str? why change to an int then to a char??

Reply
Savage 360 August 1, 2020 - 6:44 am

how is he using a break for an if statement, but when i use it it says its out of loop

Reply
Savage 360 August 1, 2020 - 6:44 am

why when i use the if operator and the break at the end, why does it always say break is out of loop

Reply
Lukshya Ganjoo August 1, 2020 - 6:44 am

Derek i am a 15 year old who is just learning to program and i do enjoy it but it seems like i am not that great at it. Any advice?

Reply
Rich B August 1, 2020 - 6:44 am

Thank you Derek.

Reply
ISLAMIC channel August 1, 2020 - 6:44 am

At 24:55 why do we use int ?

Reply
Vishal Narwal August 1, 2020 - 6:44 am

i am having trouble (in exception handling) trying to replicate the same thing in c++. please help
i do know it will be done using try catch in c++ but i dont know which excption should i handle

edit:
finally found out
here's my code if any one wants to look at and please don't forget to give suggestions
#include <iostream>
#include <cstdlib>

int main()
{
while (true)
{
try
{
char character[256];
std::cin >> character;
int number = std::stoi(character);
std::cout << number << std::endl;
break;
}
catch (std::invalid_argument const &e)
{
std::cout << "You didn't enter a Integer" << std::endl;
}
}
std::cin.ignore();
std::cin.get();
return 0;
}

Reply
Ambrosh Vaz August 1, 2020 - 6:44 am

Can someone tell me where can I find the cheat sheet?

Reply
Js Gokul August 1, 2020 - 6:44 am

Why ur taking

Reply
Tomar's world August 1, 2020 - 6:44 am

see 17:21 you got output ending with
in
but original string contains 'g' also,
i think there must be 2 different logics for both even and odd number of characters in string!?

Reply
Malic Zarith August 1, 2020 - 6:44 am

Hi Derek, I'm trying to get multiple opinions on this. I work every day on this for four hours. I've heard from many different people that one should do six to eight hours to prepare for a job. I'm working on weekends as well and not sure if they mean six hours on weekdays. Should I go up to six hours or stay at four? I'm very bad at time management, and it's taken me a great deal of time just to get to this point. I can do six, but I don't know if that's the best plan. I do tons of exercise like you said, but a half hour just wasn't getting me as far as I wanted. 4 is getting me a lot further.

Also I'm wondering if it's possible to just walk into an interview without a degree and get a job purely based on displaying skill. I'm confident in saying I understand most of the material in your videos even though I still re-watch these videos frequently. I've heard stories of college dropouts just walking in and showing a bunch of stuff they coded, but still getting a good job. Are these stories nonsense? I really just don't want a relative's help getting my first computer job. I know that's ridiculous, but I'd rather do it myself even if it means earning less for a while.

I'm not ready yet, but at some point I want to just go for a job. The point where I have enough skill to do a good job is about a year away at the rate I'm going 🙁

Reply
Vigneshwara Balachandar August 1, 2020 - 6:44 am

I don"t understand this part
"for i in range (0, (len ("samp_string")-1, 0)

Reply
Lukshya Ganjoo August 1, 2020 - 6:44 am

Derek for the problem with the spaces, the bonus one can we solve with the isalpha() function and if so could you demonstrate how

Reply
7gan7 August 1, 2020 - 6:44 am

message = str(input("Enter your message: "))
secret_massage =""
reversed_message =""
for i in range(len(message)):
secret_massage += str(ord(message[i])) +" "
print("The secret message is: ", secret_massage)
for i in range(len(secret_massage.split())):
reversed_message += str(chr(int((secret_massage.split())[i])))
print("Decrypted message: ",reversed_message)

Reply
Wim Decroos August 1, 2020 - 6:44 am

Alternative solution to the bonus work I came up with:

Original = str(input('Please enter the message you'd like to convert to unicode: '))
Secret=''
Decoded=''

for char in Original:
unicodeNumber=str(ord(char))
Secret += unicodeNumber

print ('The encoded message is: ', Secret)

i=0
while i <= (len(Secret)-2):
unicodeNumber = Secret[i] + Secret[i + 1]
if Secret[i] == '1':
unicodeNumber += Secret[i + 2]
i +=1
i += 2
Decoded += chr(int(unicodeNumber))
print('The decoded message is:', Decoded)

Reply
osama Al-Chalabi August 1, 2020 - 6:44 am

This is my code and it works, is it fine to use?

secretMessage = input("Enter a string to hide in uppercase: ")
newString = ""
#cycling through each character in a string
for i in range(len(secretMessage)):
#converting string into a UNICODE
#Converting the string to a String
#Adding the String to the newString variable
newString+= str(ord(secretMessage[i]))

#Printing the newString that is is unicode
print("Secret Message: ",newString)
#Printing the original user Input String
print("The Original Message is: ",secretMessage)

Reply
Byte Aesx August 1, 2020 - 6:44 am

Many thanks for the series.
I guess you program a lot in python or you have a lot self-control, I haven't seen any semicolon so far…

Reply
zorztrooper August 1, 2020 - 6:44 am

Why do you cycle through the characters two at a time? I have tested the program without and it doesn't work but i cant understand why.

Reply
Vijay Suresh August 1, 2020 - 6:44 am

I am very new to python and I wrote the code for the last problem like this.. not very efficient though..
text=input("enter a string to encrypt: ")
y=''
z=''
x=[]

for char in text:
y+=str(ord(char))
x.append(ord(char))

print("the encrypted message is: ",y)
print("the encrypted message as a list is: ",x)

for i in range(0,len(x)):
z+=chr(int(x[i]))

print("the decrypted message is: ",z)

Reply
Joseph Joshua Anggita August 1, 2020 - 6:44 am

I've created a 'encode/decode' system from what i've learned and some internet search for random.seed()
https://pastebin.com/yGyw8ZGs

Make sure to include the spaces in the secret string

Reply
Nikita Volkov August 1, 2020 - 6:44 am

It's another awesome lesson! Thank you, Derek Banas!

Reply
Satwik Singh August 1, 2020 - 6:44 am

At 17:05, you don't need to subtract 1 since the range function has the stop index as non inclusive i.e. it is [start, stop).

Reply
Gabriel Henrique August 1, 2020 - 6:44 am

Without realizing I did it work for upper/lower case and even for characters.

string = input("Input a message: ")

s = ''
b = ''

for c in range(len(string)):
# Deveria ter feito:
# for c in string:
# s += str(ord(c))

char = ord(string[c])
s = s + str(char)

char = chr(int(char))
b = b + char

print("Message hided: ", s)
print("Message decrypted: ", b)

Reply
Shovit Gupta August 1, 2020 - 6:44 am

thanks Derek…

Reply

Leave a Comment