Computer Science, asked by madgril17, 2 months ago

Find the programs' errors and tell me the error, the type of error, and the correction.
1)
Num1= 7
Num2=8
Sum = num1 + Num2

2)
num = float(input("Enter a number: "))
if num < 0:
print("Positive number")
elif num == 0
print("Zero")
else:
Print("Negative number")

3)
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 <= num2) and (num1 <= num3)
largest = num1
Elif (num2 >= num1) and (num2 >= num3):
largest == num2
else:
largest = num3
print("The largest number is", largest)

Answers

Answered by Equestriadash
3

1. Program 1

Given snippet:

Num1 = 7

Num2 = 8

Sum = num1 + Num2

Corrected snippet:

Num1 = 7

Num2 = 8

Sum = Num1 + Num2   #corrected line

Type of error: Name error.

Name errors usually occur when there is an unrecognized variable. In this case, num1 from the original snippet was the unknown variable since no such variable was earlier declared/assigned a value. Keep in mind that Python is a case-sensitive language so there will be a difference between num1 and Num1.

2. Program 2

Given snippet:

num = float(input("Enter a number: "))

if num < 0:

  print("Positive number")

elif num == 0:

  print("Zero")

else:

  Print("Negative number")

Corrected snippet:

num = float(input("Enter a number: "))

if num < 0:

  print("Positive number")

elif num == 0:

  print("Zero")

else:

  print("Negative number")   #corrected line

Type of error: Syntax error.

Syntax errors usually occur when there is a mistake in the syntax of a co‎de. Python has certain rules/formats to be followed when typing out commands and in order for them to work, they must be typed accordingly. In this case, conditional statements must have the statements typed out after an indentation [spacing] after the condition.

3. Program 3

Given snippet:

num1 = float(input("Enter first number: "))

num2 = float(input("Enter  second number: "))

num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 <= num3):  

   largest = num1

Elif (num2 >= num1) and (num2 >= num 3):  

   largest == num2  

else:

   largest = num3

print("The largest number is", largest)

Corrected snippet:

num1 = float(input("Enter first number: "))

num2 = float(input("Enter  second number: "))

num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):   #logical error; corrected line

   largest = num1

elif (num2 >= num1) and (num2 >= num3):   #syntax error; corrected line

   largest = num2   #name error; corrected line

else:

   largest = num3

print("The largest number is", largest)

Type of error(s): Logical, syntax and name errors.

Logical errors occur not due to the interpreter, but due to the wrong command/information given to the co‎de. If a programmer was asked to type a program to find the area of a rectangle, and he gave the calculation for the area as 2 × (Length + Breadth), that would be wrong and hence result in an error in the logic of the coding. In this case, according to the first conditional statement, the condition was trying to check if num1 was lesser than num2 and num3, when our aim was to find the largest number and not the smallest.

As mentioned above, syntax errors occur due to an error in the syntax of a command. In this case, elif was typed as Elif, which does not make it the same as elif since Python is a case-sensitive language.

Again, as mentioned above, name errors occur when there is an unknown variable in the co‎de that was not earlier declared/assigned a value. In this case, after the condition, the program was trying to test the equality of variables largest and num2, which (A) there was no such variable largest declared earlier, and (B) == and = are both different in Python. == is used to test for equality whereas = is used for assignment purposes. For example, we could store the value 7 into a variable x by simply typing x = 7. We could test the equality and see if x is equal to 7 or not by typing x == 7 which would return True since it is actually true as that was the value assigned to x. If we typed x == 6, it would return False as it was not 6, but 7 that we assigned to x.

Similar questions