Task:
Print "user_num1 is negative." if user_num1 is less than 0. End with newline.
Assign user_num2 with 5 if user_num2 is greater than 8. Otherwise, print "user_num2 is less than or equal to 8.". End with newline.
Problem:
I’ve attached my code and for some reason when user_num2 = 8 it does not output the print statement as depicted in the output image. What am I doing wrong?
Attempted code:
user_num1 = int(input())
user_num2 = int(input())
if user_num1 < 0:
print('user_num1 is negative.')
elif user_num2 <= 8:
print('user_num2 is less than or equal to 8.')
else:
user_num2 = 5
print('user_num2 is', user_num2)
Solution
This is what I understand from the text:
user_num1 and user_num2 tests are independant, so each has its own "if" statement. Otherwise the user_num2 test is not executed when user_num1 < 0.
user_num1 = int(input())
user_num2 = int(input())
if user_num1 < 0:
print('user_num1 is negative.')
if user_num2 <= 8:
print('user_num2 is less than or equal to 8.')
else:
user_num2 = 5
print('user_num2 is', user_num2)