Python Tutorial: Find Syntax error(s)

Sunday, 14 August 2022

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

18. Write the output of the given code

def Check (K, L=70):

      if(K > L) :

         K-=L

      else:

         K +=L

 

M=100

N=40

Check (M, N)

print (M, "#",N)

Check (M)

print ( N ,"#" ,M)

Ans: 100 # 40

         40 # 100

19. Write the output of the given codex:

y=7

def display():

       global x

       x = 10

       if x >= 7 :

             y = 9

             print (y)

display()

print(x)

Ans: 9

         10

20. Write a function Display() which do not accept any parameter and return none.

Ans: def display():

               print()

               return none

print(display())

                 OR

def display():

               print()

print(display())

21. Answer the following questions based on the given script/code.

def show(a):

        a=10

        a = a+3

             return a

b=100

res=show(b)

(a) Name the variable used as parameter.                             Ans: a 

(b) Name the variable used as an argument                          Ans: b

(c) Write function header statement.                                      Ans: def show(a)

(d) Which statement will return a value?                               Ans: return a

(e) Name a local variable(s).                                                      Ans: a

(f) Name a global variable(s).                                                    Ans: b

(g) Write function call statement.                                            Ans: res=show(b)

(h) Variable 'a' will return the value to variable_____.          Ans: res

(i) Name formal parameter.                                                   Ans: a

22. Answer the following questions based on the given script/code.

def show4 (a,b):

       print(a+b)

x=shows (10,15)

print(x)

(a) Name local variable(s).                                                     Ans: a,b

(b) Name global variable(s).                                                   Ans: x

(c) _________is built-in function.                                           Ans: print()

(d) _________is user-defined function.                                 Ans: show4()

(e) Name actual parameter(s).                                               Ans: 10, 15

(f) Name formal parameter(s).                                               Ans: a,b

23. Write the function definition for the following:

(a) A function ADD() accepts two parameters.

(b) A function ADD() accepts two parameters and returns a single value.

(c) A function Vote_Casting() will accepts age and citizen as parameter returns Boolean value.

(d) A function Show () accepts no parameters and returns nothing.

Ans: (a) def ADD(a,b):

         (b) def ADD(a,b):

                      Return a

         (c) def Vote_Casting(age, citizen):

                      return True or return False

          (d) def Show():

                       Return None

24. Write the output of the following script/code

(a) def show5():

             print("Hello")

             show5a()

      def show5a():

            print("Bye")

      show5a()

(b) def show5():

             print("Hello")

      show5a()

     def show5a():

             print("Bye")

      show5()

Ans: (a) Bye

         (b) Bye

               Hello

25. Write the output of the given program:

     def displaylist (lst):

       print("Even Elements of the list")

       for i in range(0,len(lst)):

            if lst[i]%2==0:

                    print(lst[i])

lst1=[23,45, 12, 10, 16, 17]

displaylist (lst1)

Ans: Even Elements of the list

         12

         10

         16

26. Write the output of the given program:

def loop1(n) :

       sum=0

       i = 1

       while i<=n:

            sum += i

            i +=5

       print("sum =", sum)

 

x = 30

loop1(x)

Ans: sum= 81

27. Write the output of the given python program:

(a) 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))

(b) for i in range(-10, -100, -30):

             print(i)

      print(i)

(c) x=7

      def new_value():

             x=70

            print("x=",x)

      print("x=",x)

      new_value()

      print("x=",x)

Ans: (a) 3

               15

(b) -10

      -40

      -70

      -70

(c) x= 7

      x= 70

      x= 7

29. Find the output

def show (a, b):

       return a+b

print("Addition is: ", show("679",  "str"))

Ans: Addition is:  679str

30. What will be the scope of the variables a, b, c, d, e?

def show():

       a = 10

       b = 15

       c = 30

def show1():

       x="Together With"

       y = 100

       z = 10

Ans: Variable a,b and c has local scope of show().

         Variable x and y has local scope of show().

         Variable z has global scope.

Find Syntax error(s)

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

A) Value=30

     for VAL in range(0,Value)

         If val%4 = =0:

                   print(VAL*4)

         Elseif val%5= =0:

                   print (VAL+3)

       else

      print(VAL+10)

Solution:

    Value=30

     for VAL in range(0,Value):                   # Error 1

           if val%4 = =0:                                    # Error 2

                   print(VAL*4)                

           elif val%5= =0:                                 # Error 3

                   print (VAL+3)

          else:                                                      # Error 4

      print(VAL+10)

 

B) def calculate(num=5):

for x in Range(num):

if x %% 2= =0

      print(x*2)

                      Else if x// 2 = =1:

      print(“ * ” * x)

      print(“END”)

 

#_main_

Calculate(10,12)

Solution:

    def calculate(num=5):

             for x in range(num):

                        if x % 2= =0:

                              print(x*2)

                       elif x// 2 ==1:

                               print(“ * ” * x)

                               print(“END”)

 

#_main_

Calculate(10) or   calculate(__)

 

C) def factorial():

             Fact=1

             X = input(“Enter number : ”)

            if X <= 10

                       While X>0:

                                       Fact =* X

                                       X=X-1

        else

                      print(“Number Exceeds”)

      

      #___main__

      factorial()

      print(Fact)

Solution:

def factorial():

             global Fact

 Fact=1

             X = int(input(“Enter number : ”))

            if X <= 10:

                       while X>0:

                                       Fact *= X

                                       X=X-1

        else

                      print(“Number Exceeds”)

     

      #___main__

      factorial()

      print(Fact)

 

D) L1=[33, 22, 12, 54, 34, 26]

     for index in L1

                     if index not in L2:

                                        index.append(L2)

     print(L1)

     print(L2)

Solution:

     L1=[33, 22, 12, 54, 34, 26]

     L2=[]

     for index in L1:

                     if index not in L2:

                                        L2.append(index)

     print(L1)

     print(L2)

 

E) T=(45, 21, 35, 26, 53, 33, 41)

     while I < 7:

                      if T[I]%3 = =0 And T%7 ==0:

                            print(T[1]/2)

                     else:

                            T[1]=T[1]+10

                           print(T[1])

     I+=1

     else:

     print(“Loop ends")

Solution:

    T=(45, 21, 35, 26, 53, 33, 41)

    I=0

    while I < 7:

            if T[I]%3 = =0 and T%7 ==0:

                            print(T[1]/2)

                     else:

                            print(T[1]=T[1]+10)

           I+=1

     else:

           print(“Loop ends")

 

F) c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

    for b in range(10)

                           if (b % 2) = 0:

                          sum += c[d]

    print(sum)

Solution:

    c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

    sum=0

    for b in range(10)

                           if (b % 2) = = 0:

                          sum += c[b]

    print(sum)

 

G) M=1

     If M%2 =  1:

                          for N in range(1, 10, 2)

                                print(VAL)

   else:

                         for N in range(2, 10, 2.5):

                            print(VAL)

   else:

                VAL=M+N

   print(VAL)

Solution:

     M=1

     VAL=0

     if M%2 = =  1:

                          for N in range(1, 10, 2)

                                print(VAL)

      else:

                         for N in range(2, 10, 2):

                            print(VAL)

      else:

                         VAL=M+N

                          print(VAL)

 

H) string = “Hello”

      for j in range len(string))

               if string[j] = = “”;

                          print(“Space”)

               elif:

                        s= lower(string[i])

     print(“ String is :+s)

Solution:

      string = “Hello”

      for j in range len(string))

               if string[j] = = “”;

                          print(“Space”)

               elif string[j].upper():

                      s= string[j].lower()

     print(“ String is :+s)

 

I) m=[43, 44, 45 ]

    x=0

    while x<n:

                if x%2< > 0:

                         print(m*4)

               elseif x%5=0:

                         print(x**3)

               else:

                        print(x*10)

              x+= 1

Solution:

    m=[43, 44, 45 ]

    x=0

    while x< len(m):

                if x%2 != 0:

                         print(m*4)

               elif x%5= = 0:

                         print(x**3)

               else:

                        print(x*10)

              x+= 1

 

J) def ret_sum(limit)

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

                                            if I % 3 = 1:

                                                   S+=I

                    Return S

 

     #__main___

     Limit=10

     print(ret_sum(limit)

     ret_num(50)

     print(S)

Solution:

    def ret_sum(limit):

                   S=0

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

                                            if I % 3 = 1:

                                                   S+=I

                    return S

 

     #__main___

     Limit=10

     print(ret_sum(limit)

     S = ret_num(50)

     print(S)

 

K) Num = int(input(“Enter number : ”))

     Sum=0

     for i in range(10, Num, 3)

                           Sum+=i

                     if i % 2=0:

                              print(i*2)

                      Else:

                             print(i*3)

         print sum

Solution:

    Num = int(input(“Enter number : ”))

     Sum=0

     for i in range(10, Num, 3)

                           Sum+=i

                     if i % 2= =0:

                              Print(i*2)

                      else:

                             Print(i*3)

         print(sum)

 

L) m=(21, 34, 36)

     n=0

     while n < m:

           if n % 2!=0:

                       print(m*4) elseif  x%4=0:

                       print(n*3) else:

                       print(n*10)

           x+=1

Solution:

     m=(21, 34, 36)

     n=0

     while n < len(m):

           if n % 2!=0:

                       print(m*4)

           elif n%4= =0:

                       print(n*3)

           else:

                       print(n*10)

           x+=1

 

M) A=10

      3=B

      if A < B:

                     for C in range(A, B, 2):

                                print(C*C, endl= “ ”)

                     print()

     elif  B < A:     

                    for C in range(A, B, 3)

                                          print(C**C, sep= “:”)

                    else A = =B:

                            pass

Solution:

A=10

B=3

if A > B:

             for C in range(A, B, -2):

                        print(C*C, end= " ")

             print()

elif  B < A:     

            for C in range(A, B, -3):

                                  print(C**C, sep= ":")

            else:

                    pass

 

N) Text=Input(“Enter text :”)

      For I in range(len(Text)):

           if Text[I]= = ‘e’ OR newText[I] = = ‘o’:

           newText+=Text[i].toupper()

     print(Text, newText, sep= “:”)

Solution:

Text=input("Enter text :")

newText=''

for I in range(len(Text)):

   if Text[I]=='e' or Text[I]=='o':

          newText+=Text[I].upper()

print(Text, newText, sep= ":")

O) S= “TODAY”

     for I in range(1;len(S);2):

     if S[I]>= ‘D’ and S[I]<= ‘Y’:

                        S2[:]= “#”

     input(S2)

Solution:

S= "TODAY"

S2=""

for I in range(1,len(S),2):

    if S[I]>= "D" and S[I]<= "Y":

            S2=S[I]+"#"

print(S2)

 

P) Cost = (250, 200, 325, 280)

     for x in range(0, 4):

                     if Cost[x]>=300:

                                      discount=15

                                      Cost[x]=Cost[x]-Cost[x]*discount

                    else if Cost[x]>200:

                                      Discount=10

     Cost[i]=Cost[i]-Cost[i]*discount

                     else:

                                     print(“No Discount”)

Solution:

Cost = [250, 200, 325, 280]

for x in range(0, 4):

             if Cost[x]>=300:

                              discount=15

                              Cost[x]=Cost[x]-Cost[x]*discount

             elif Cost[x]>200:

                              discount=10

                              Cost[x]=Cost[x]-Cost[x]*discount

             else:

                             print("No Discount")

print(Cost)

No comments:

Post a Comment