0% found this document useful (0 votes)
0 views

Find Syntax error

The document contains various Python code snippets with syntax errors and their corrections. It includes examples of functions, variable assignments, and control structures, along with expected outputs for specific code executions. The document also discusses random number generation and statistical calculations using libraries.

Uploaded by

mahimishra054
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Find Syntax error

The document contains various Python code snippets with syntax errors and their corrections. It includes examples of functions, variable assignments, and control structures, along with expected outputs for specific code executions. The document also discusses random number generation and statistical calculations using libraries.

Uploaded by

mahimishra054
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Find Syntax error(s)

1) Rewrite the following python code after removing any/ all syntactical errors with each
correction underlined:

(a) def guess()

print("Hello")

print(a)

a=5

var=2

guess

(b) def add:

total=0

n=input("Enter a number:

m=input("Enter another number: ")

total=m+n

print total

add()

(c) def func(a+b):

tot= a+b

print(tot)

m=10

n=20

fun(m,p)

(d) def f1(b):

if b>0 then:

return “Positive”
else return "Not Positive"

m=int(input("Enter an number: "))

msg=f1()

print(msg)

(e) def isMultiple (a,b):

if a%b= 0:

return true

else return false

m=int(input("Enter first number: "))

n=int(input("Enter second number: "))

if isMultiple[m,n]:

print("m is a multiple of n")

else print(m, "is not a multiple of n")

(f) find HCF (a, b):

h=a

while b%h!=0 OR a%h!=0:

h-=1

return h

m=int(input("Enter first number: "))

n=int(input("Enter second number: ))

HCF (m.n)=h

print("HCF=",h)

Ans

(a) def guess(): #error 1

print("Hello")

a=5 #error 2
print(a) #error 3

var=23

guess() #error 4

(b) def add(): #error 1

total=0

n=input("Enter a number:") #error 2

m=input("Enter another number: ") #error 3

total=m+n

print(total) #error 4

add()

(c) def func(a,b): #error 1,2

tot= a+b

print(tot)

m=10

n=20

func(m,n) #error 3,4

(d) def f1(b): #error 1

if b>0: #error 2

return "Positive"

else: #error 3

return "Not Positive"

m=int(input("Enter an number: "))

msg=f1(m) #error 4

print(msg)

(e) def isMultiple (a,b): #error 1


if a%b== 0: #error 2

return True error 3

else:

return False error 4

m=int(input("Enter first number: "))

n=int(input("Enter second number: "))

if isMultiple(m,n): error 5

print("m is a multiple of n")

else:

print(m, "is not a multiple of n") #error 6

(f) def HCF (a, b): #error 1

h=a

while b%h!=0 or a%h!=0: #error 2, 3

h-=1

return h

m=int(input("Enter first number: "))

n=int(input("Enter second number: "))

h=HCF (m,n) #error 4

print("HCF=", h) #error 5

2. Find the errors and rectify the script:

def show(n, string= “Good”, k=5):

return display(string)

return n

def display (string):

return string==str(5)

print(show(“Children’s Day”))
print (display (string=’true’):)

print (show (5, “Good”))

Ans:

def show(n, string= "Good", k=5):

return display(string)

return n #Cannot return 2 values

def display (string):

return string==str(5) # indentation

print(show("Children’s Day"))

print (display (string="true")) # : to be removed

print (show (5, "Good"))

3. Rewrite the following python code after removing any/all syntactical errors with each
correction underlined:

Def displaylist (lst):

print("Even Elements of the list")

for i in Range (0, len(1st)):

if lst[i]%2=0:

print (lst[i])

lst1=eval (input("Enter the list"))

displaylist()

Ans:

def displaylist (lst): # error 1

print("Even Elements of the list")

for i in range (0, len(lst)): # error 2

if lst[i]%2==0:

print (lst[i])

lst1=eval(input("Enter the list"))


displaylist(lst1)

4. Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code.

def Sum(Count) #Method to find sum

S=0

for I in Range (1, Count+1):

S+=I

RETURN S

print (Sum[2]) #Function Call

print (SUM[5])

Ans:

def Sum(Count): #Method to find sum

S=0

for I in range (1, Count+1):

S+=I

return S

print (Sum(2)) #Function Call

print (Sum(5))

5. What will be the output of the following Python code?

x = 50

def func():

global x

print('x is', x)

x=2

print(“Changed global x to”, x)

func()

Ans: x is 50
Changed global x to 2

6. What will be the output of the following Python code?

def say (message, times = 1):

print(message*times)

say(‘Hello’)

say( ‘World’, 5)

Ans: Hello

WorldWorldWorldWorldWorld

7. What will be the output of the following Python code?

def func (a, b = 5, c = 10) :

print(‘a is', a, 'and b is', b, 'and c is’ , c)

func (3, 7)

func (25, c = 24)

func(c=50,a=100)

Ans: a is 3 and b is 7 and c is 10

a is 25 and b is 5 and c is 24

a is 100 and b is 5 and c is 50

8. What will be the output of the following Python code?

def f1():

X = 15

print(X)

X = 12

f1()

Ans : 15

9. What will be the output of the following Python code?

x = 12
def f1(a, b = x):

print (a,b)

x = 15

f1(4)

Ans: 4 12

10. What will be the output of the following Python code?

def f():

global a

print(a)

a = “hello”

print(a)

a = “world”

f()

print(a)

Ans: world

hello

hello

11. Write the output of the following Python code :

def Update (x =10):

x += 15

print(“x=”, x)

x=20

Update()

print(“x=”, x)

Ans: x= 25

x= 20
12. Write the output of the following Python code:

def div5(n):

if n%5==0:

return n* 5

else:

return n + 5

def output ( m= 5) :

for i in range (0, m):

print(div5(i),'@',end="")

print()

output(7)

output()

output(3)

Ans: 0 @6 @7 @8 @9 @25 @11 @

0 @6 @7 @8 @9 @

0 @6 @7 @

13. Write the output of the following Python code:

def func(b):

global x

print(“Global x =”,x)

y = x+b

x=7

z=x-b

print(‘Local x =’ ,x)

print (‘y =’ ,y)

print(‘z=’, z)
x=5

func(10)

Ans: Global x = 5

Local x = 7

Y = 15

z = -3

14. Write the output of the following Python code:

def func (x, y = 100):

temp = x+y

x+=temp

if (y!=200) :

print(temp,x,x)

a=20

b=10

func(b)

print(a,b)

func(a,b)

print (a,b)

Ans: 110 120 120

20 10

30 50 50

20 10

15. Write the output of the given code:

def Convert (Old) :

l=len(Old)

New=""
for i in range (0,l):

if Old[i].isupper():

New=New+Old[i].lower()

elif Old [i].islower () :

New=New+Old [i].upper()

elif Old[i].isdigit():

New=New+"*"

else:

New=New+ "%"

return New

Older="InDIa@2020"

Newer= Convert(Older)

print ("New string is : ", Newer)

Ans: New string is : iNdiA%****

16. Write the output of the given code:

def display(n):

sum=5

i= 1

while i<=n:

sum += i

i+= 5

sum=sum-2

print("Value = ", sum," i= ",i)

x = 30

display(x)
Ans: Value = 74 i= 31

17. Write the output of the given code:

x = 15

def change():

#using a global keyword

global x

# increment value of a by 5

x=x+5

print("Value of x inside a function : ", x)

change()

print("Value of x outside a function :", x)

Ans: Value of x inside a function : 20

Value of x outside a function : 20


Python Program to generate one-time password (OTP)
import math, random
digits = "0123456789"
OTP = ""
for i in range(4) :
OTP += digits[math.floor(random.random() * 10)]
print("OTP of 4 digits:", OTP)
Number Guessing Game using Python

import random

n = random.randrange(1,10)

guess = int(input("Enter any number: "))

while n!= guess:

if guess < n:

print("Too low")

guess = int(input("Enter number again: "))

elif guess > n:

print("Too high!")
guess = int(input("Enter number again: "))

else:

break

print("you guessed it right!!")

Q1. Consider the code given below:


import random
r = random.randrange (100, 999, 5)
print(r, end = ' ')
r = random.randrange(100, 999, 5)
print(r, end = ' ')
r = random.randrange (100, 999, 5)
print(r)
Which of the following are the possible outcomes of the above code? Also, what can be
the maximum and minimum number generated by line 2?
(a) 655, 705, 220 (b) 380, 382, 505 (c) 100, 500, 999 (d) 345,
650, 110
Ans : (a) & (d)
Q2. Consider the code given below:
import random
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r)
Which of the following are the possible outcomes of the above code? Also, what can be
the maximum and minimum number
generated by line 2?
(a) 12 45 22 (b) 100 80 84 (c) 101 12 43 (d) 100 12
10
Ans : (a)
Q3. Consider the code given below:
import random
r= random.random() * 10
print(r, end = ' ')
r = random.random() * 10
print(r, end =' ')
r = random.random() * 10
print(r)
Which of the following are the possible outcomes of the above code? Also, what can be
the maximum and minimum number generated by line 2?
(a) 0.5 1.6 9.8 (b) 10.0 1.0 0.0 (c) 0.0 5.6 8.7 (d) 0.0 7.9
10.0
Ans : (a) & (c)
Q4. Consider the code given below:
import statistics as st
v = [7, 8, 8, 11, 7, 7]
m1 = st.mean(v)
m2 = st.mode(v)
m3 = st.median(v)
print(m1, m2, m3)
Which of the following is the correct output of the above code?
(a) 7 8 7.5 (b) 8 7 7 (c) 8 7 7.5 (d) 8.5 7 7.5
Ans : (c)
Q5. What possible output(s) are expected to be displayed on screen at the time of execution
of the program from the following code? Also, specify the maximum values that can
be assigned to each of the variable Lower and Upper
import random
AR = [20, 30, 40, 50, 60, 70]
Lower = random.randint(1,3)
Upper= random.randint(2,4)
for K in range (Lower, Upper+1):
print(AR[K], end=”#”)
Output Options:
(a) 10#40#70# (b) 30#40#50# (c) 50#60#70# (d)
40#50#70#

Ans : (b), Minimum value of Lower: 3, Maximum value of Upper: 4


Q6. What possible output(s) are excepted to be displayed on the screen after executing the
following code?
import random
Lst=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
first= random.randrange(2,5)
second = random.randrange(1,3)
for L in range (second, first):
print(Lst[L], end = “@”)
Output Options:
(a) 1@2@3@ (2) 0@1@2@3@ (c) 2@3@ (d) 2@3@4@

Ans: (a) & (c)


Q7. What is possible output(s) is/are expected to be displayed on screen after execution of
the following program?
import random
Guess=65
for a in range(0,1):
for b in range (1,5):
New = Guess+random.randrange(0,b)
print(chr(New), end= ‘ ’)
(a) A B B C (b) A C B A (c) B C D A (d) C A B D

Ans: (a)
Q8. What is possible output(s) is/are expected to be displayed on screen after execution of
the following program?
import random
MIN= 25
SCORE=5
for i in range(1,5):
Num=MIN+random.randrange(0,SCORE)
print(Num, end = “&”)
SCORE -=1
Output Options:
(a) 29&26&28&27& (b) 25&26&27&28& (c) 29&25&27&25& (d)
25&26&25&26&

Ans: (c) & (d)


Q9. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the maximum values
that can be assigned to the variables X and Y.
import random
X = random.randrange(0,3)+2
Y = random.randrange(1,3)+2
for i in range(X):
print("#", end= “”)
print("-", end=" ")
for i in range(Y):
print(“@”, end="")
Output Options:
(a) ##-@@@ (b) ###-@ (c) ######-@@@ (d) ###-@@@@

Ans: (d), Maximum value for is X: 4, , Manimum value for is Y: 4,


Q10. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the maximum values
that can be assigned to a variable val, in each iteration.
import random
msg = "VIRTUAL"
index = len(msg)-1
while msg[index] != 'R':
val = random.randrange(2,index)
print(msg[val]+"#",end="")
index = index-1
(a) A#R#U#R# (b) A#R#T#T# (c) U#A#R#R# (d) U#R#T#R#

Ans : (d) , Max value=5


Q11. What possible output(s) are expected to be display execution of the following code?
import random
HIGH = 20
NUM = 15
for i in range(1,5):
N = HIGH+random.randint(11, NUM)
print(N, end = “:”)
NUM - = 1
Outputs:
(a) 35:31:34:32: (b) 35:34:33:32: (c)33:34:33:32: (d) 32:31:35:31:

Ans: (b)
Q12. What possible output(s) are expected to be displayed on one of the execution of the
following code is specify the maximum and minimum values which can be assigned to
the ‘Target’ by randrange() function of random module.
import random
X= [11000, 999,99, 9]
Target = random.randrange(0,4)
for i in range(Target):
print(X[i], “#”)
(a) 1000# (b) 999# (c) 1000# (d) 999#
999# 99# 1000# 99#
99# 9# 999#
Ans: (a) , Maximum:3 , Minimum : 0
Q13. From the following Python code shown, below, find out the possible output(s) from
the suggested options.
import random
def week_days():
days=[“Mon”, “Tue”, “Wed”, “Thu”, “Fri”]
v = random.randrange(len(days)-1)
for i in range(v,0,-1):
print(days[i], “-“, i+1)
week_days()
(a) Thu- 4 (b) Wed-3 (c) Wed -3 (d) Fri-5
Wed-3 Tue-2 Tue-2 Thu-4
Tue-2 Mon-1
Ans : (a) & (b)
Q14. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to both the variables Beg and End.
import random
score= [11, 22, 33, 44, 55]
Beg = random.randrange(3)
End = random.randint(2,3)
for C in range(Beg, End):
print(Score[C],'#')
(a) 11# (b) 22# (c) 22# (d) 33#
22# 33# 33# 44#
33# 44#
Ans: (a), (c) Max value to a variable Beg: 2, Min value to a variable End: 3
Q14. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to both the variables Y.
import random
X = random.randrange(1,4)
Y= random.random()
print(“%2d”, %(Y+X), “@”, X)
(a) 00 @ 0 (b) 01 @ 1 (c) 02 @ 2 (d) 04 @ 4
Ans: (b) & (c), Max value to Y:0, Min value to Y: 1
Q15. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code?
import random
n=4
while n>1:
val= random.randint(2,n)
for m in range(val)
if m%2= = 0:
print(m*2, end= “:”)
n=1
Output Option
(a) 0:4:0:0: (b) 0:0:0: (c) 0:4:0:4:0 (d) 0:4:0:4
Ans:
Q16. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the first and last
values that can be assigned to a the variables val in the last iteration.
import random
msg= “Luck”
x=1
for a in msg:
val=random.randint(0, len(msg)-x)
print(mag[val]+ “:”, end= “”)
x=x+1
(a) c:k:u:k: (b) k : u : c : c : (c) l : u : u: c : (d) l : c : u : l :
Q17. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to the variables ‘Assigned’.
import random
Ar= [10,7]
Assigned = random.randrange(10)+1
for C in range(0,2,1):
R=random.randint(1,2)-1
print(Ar[R]+Assigned, “@”, end= “ ”)
(a) 11@8@ (b) 20@21@ (c) 21@20@ (d) 8@11@
Q18. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code? Also, specify the maximum values
that can be assigned to the variables ‘high’.
import random
Ar=[11, 22, 33, 44, 55, 66 ]
low=random.randint(1,3)
high=random.randint(2,4)
for c in range(low, high+1):
print(Ar[c], end= “:”)
(a) 11 : 22 : 33 : (b) 22 : 33 : (c) 11 : 22: (d) 44 : 55 : 66
:
Q19. What possible output(s) is/are expected to be displayed on screen at the time of
execution of the program from the following code?
from random import randint
Vibgyor=[[‘V’, ‘Violet’], [‘I’, ‘Indgo’], [‘B’, ‘Blue’], [‘G’, ‘Green’],[‘Y’, ‘Yellow’],[‘O’,
‘Orange’],[‘R’, ‘Red’]]
for i in range(3):
first=randint(0,1)
last=randint(1,2)+1
print(Vibgyor[last-first], end= ‘:’)
(a) [‘G’, ‘Green’]: [‘G’, ‘Green’]: [‘Y’, ‘Yellow’]:
(b) [‘G’, ‘Green’]: [‘B’, ‘Blue’]: [‘G’, ‘Green’]:
(c) [‘V’, ‘Violet’]: [‘B’, ‘Blue’]: [‘B’, ‘Blue’]:
(d) [‘I’, ‘Indgo’]: [‘B’, ‘Blue’]: [‘B’, ‘Blue’]:

Q20. What are the possible outcome(s) executed from the following code? Also specify the
maximum and minimum values that can be assigned to variable NUMBER.

import random

STRING="CBSEONLINE"

NUMBER = random.randint (0, 3)

N=9

while STRING[N] != "L":

print (STRING[N] + STRING[NUMBER] + "#", end = " ")

NUMBER = NUMBER +1

N=N-1

(a) ES#NE#IO# (b)LE#NO#ON# (c)NS#IE#LO#


(d)EC#NB#IS#

Ans: Outcomes are (i)

Maximum value of NUMBER is 6.


Minimum value of NUMBER is 3.

Because Minimum value of NUMBER from random.randint (0, 3) is 0 and while loop will
run only 3 times. So, Minimum value of NUMBER is 3.

Maximum value of NUMBER from random.randint (0, 3) is 3 and while loop will run only
3 times. So, Maximum value of NUMBER is 6.

Q21. Consider the following code:

import random
print (int( 20 + random.random()*5), end =" ")
print (int( 20+ random.random()*5), end =" ")
print (int(20 + random.random()*5), end = " ")
print (int( 20 + random.random()*5))
Find the suggested output options (i) to (iv). Also, write the least value and highest value
that can be generated.

(i) 20 22 24 25 (ii) 22 23 24 25 (iii) 23 24 23 24 (iv) 21 21 21 21


Q22. Consider the following code:

import random
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10), end = ' ')
print (100 + random.randint(5, 10))
Find the suggested output options (i) to (iv). Also, write the least value and highest value
that can be generated.

(i) 102 105 104 105 (ii) 110 103 104 105 (iii) 105 107 105 110 (iv) 110 105 105
110

Q23. What are the possible outcome(s) executed from the following code ? Also specify the
maximum and minimum values that can be assigned to variable PICKER.
import random
PICK = random.randint (0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY :
for j in range(1, PICK):
print(I, end =" ")
print()
(i) DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
(ii) DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
(iii) DELHI
MUMBAI
CHENNAI
KOLKATA
(iv) DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA

You might also like