109k views
3 votes
Conditional expression: Print negative or non-negative. Create a conditional expression that evaluates to string "negative" if user_val is less than 0, and "non-negative" otherwise.

•Sample:
-input: -9
-output: -9 is negative
1 user_val = int(input())
2
3 cond_str =
4
5 print(user_val, 'is', cond_str)

1 Answer

1 vote

Final answer:

You can use a conditional expression in Python to determine if a number is negative or non-negative.

Step-by-step explanation:

In Python, you can use a conditional expression to determine whether a number is negative or non-negative. You can write a conditional expression using the syntax: result = 'negative' if user_val < 0 else 'non-negative'. This expression evaluates to the string 'negative' if user_val is less than 0, and 'non-negative' otherwise.

Here's an example:

user_val = int(input())
cond_str = 'negative' if user_val < 0 else 'non-negative'
print(user_val, 'is', cond_str)

If the user enters -9 as the input value, the program will output '-9 is negative'.

User Snympi
by
7.6k points