159k views
4 votes
4.8.1: 1f-else statement: Fix errors. Find and fix the error in the if-else statement 1 usernum - Int(input) # Program will be tested with values: 1, 2, 3, 0. 2 3 if user_num= 2: 4 print("Num is equal to two 5 else: 6 print('Num is not two) 7

User Wypul
by
7.5k points

2 Answers

7 votes

Final answer:

The error in the if-else statement is corrected by changing the assignment operator '=' to the comparison operator '==', fixing syntax issues with quotation marks and adding missing parenthesis.

Step-by-step explanation:

The given code snippet contains a syntax error related to the if-else statement. To fix the error, one needs to correct the assignment operator '=' to a comparison operator '=='. Additionally, there are a few minor issues including missing parenthesis and inconsistent quotation marks which can cause syntax errors when the code runs. Here's the corrected version of the code:

user_num = int(input())
if user_num == 2:
print("Num is equal to two")
else:
print('Num is not two')

This will check if the value entered by the user is equal to 2 or not, and will output the corresponding message.

User Onupdatecascade
by
8.0k points
4 votes

The corrected code is shown below

python

# Program will be tested with values: 1, 2, 3, 0.

user_num = int(input()) # convert input to integer

if user_num == 2:

print("Num is equal to two")

else:

print('Num is not two')

The errors in the main code was that; The user_num variable was not being converted to an integer before being compared to 2. This caused the code to print "Num is equal to two" for all input values, because the comparison user_num == 2 is always true when user_num is a string.

Also, The closing parenthesis for the print() statement in the else block was missing. This caused a syntax error.

See text below

1f-else statement: Fix errors. Start Find and fix the error in the if-else statement

1 usernum - Int(input) # Program will be tested with values: 1, 2, 3, 0.

2

3 if user_num= 2:

4 print("Num is equal to two

5 else: print('Num is not two)

6

7

8

4.8.1: 1f-else statement: Fix errors. Find and fix the error in the if-else statement-example-1
User AnupamD
by
7.6k points