Bakliwal Tutorials
------------------------------------------------------------------------------------------------------------------------------------------
---
Some Important Codes Discussed in Lecture 3
For Loop Examples
myList = [2, 3, 5, 7, 11]
for p in myList:
print(p)
myList = range(1, 21)
for i in myList:
cube = i*i*i
print("The cube of "+str(i)+" is "+str(cube))
Modified Caesar Cipher for Lecture 3
print("We can now encode entire sentences in Caesar Cipher")
sentence = input("Enter a sentence to encode: ")
codedSentence = ''
for c in sentence:
o = ord(c)
coded_o = o+1
if coded_o == 91:
coded_o = 65
elif coded_o == 123:
coded_o = 97
newC = chr(coded_o)
codedSentence = codedSentence + newC
print("Encoded Sentence:")
print(codedSentence)
Bakliwal Tutorials
------------------------------------------------------------------------------------------------------------------------------------------
---
Modified Number Guessing Game for Lecture 3
import random
myNumber = [Link](1, 51)
print(myNumber)
print("Welcome to the number guessing game...")
print("I have decided upon a number from 1 t0 50 in my mind. You
need to guess it within 5 tries.")
for i in range(1, 6):
guessStr = input("Enter your guess "+str(i)+":")
guess = int(guessStr)
if myNumber == guess:
print("Good Work! You guessed the number correctly.")
break
elif myNumber < guess:
print("Your guess is higher than the number i have in
mind")
else:
print("Your guess is lower than the number I have in mind")
if myNumber != guess:
print("Hard Luck This Time… Do try again...")
While Loop Example
inp = ''
while inp != 'q':
inp = input("Enter a letter: ")
print("You typed "+inp)
‘break’ with for
for i in range(10):
print(i)
if i > 5:
break
Bakliwal Tutorials
------------------------------------------------------------------------------------------------------------------------------------------
---
‘break’ with while
while True:
s = input("Type any word:")
print("You typed "+s)
if(s == 'quit'):
break
Modified Rock Paper Scissors in Lecture 3
import random
movesList = ['r', 'p', 's']
userWinList = ['rs', 'sp', 'pr']
tieList = ['rr', 'pp', 'ss']
userScore = 0
compScore = 0
while True:
compMove = [Link](movesList)
userMove = input("Enter your move (r/p/s/quit):")
if userMove == 'quit':
break
combMove = userMove+compMove
print("I had played "+compMove)
if combMove in tieList:
print("So it's a tie !")
userScore = userScore + 1
compScore = compScore + 1
elif combMove in userWinList:
print("YOU WIN !!! Congrats...")
userScore = userScore + 1
else:
print("Yeeee !!! I win...")
compScore = compScore + 1
print('The final score tally is - ')
print('Your Score: '+str(userScore)+"\t\tMy Score:
"+str(compScore))
Bakliwal Tutorials
------------------------------------------------------------------------------------------------------------------------------------------
---
List of all divisors
numStr = input("Enter a positive integer:")
num = int(numStr)
listOfDivisors = []
for i in range(1, num+1):
if num%i == 0:
[Link](i)
print("The list of all divisors of "+numStr+" is:")
print(listOfDivisors)
Identifying Prime Numbers
numStr = input("Enter a natural number and I'll tell you if it is
prime or not:")
num = int(numStr)
msg = "The number entered by you is a prime number."
for i in range(2, num):
if num%i == 0:
msg = "The number entered by you is a composite number."
print(msg)
List of Primes
listOfPrimes = []
usrNum = 1
while len(listOfPrimes)<5:
listOfDiv = []
for x in range(1, usrNum+1):
rem = usrNum % x
if rem == 0:
[Link](x)
if len(listOfDiv) == 2:
[Link](usrNum)
usrNum = usrNum + 1
print(listOfPrimes)
#print(len(listOfPrimes))