Python Forum

Full Version: Trouble with assigning a string value in conditional statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am having trouble assigning a string value (namely, "Odd" or "Even") to val in my conditional operator. I am to have a user input a number, use a conditional operator to determine whether the number is odd or even, assign "Odd" or "Even" to a variable, and then print the result using that assigned variable.

Thanks in advance for your help!

num = int(input("Enter a whole number; this program will determine if your value is odd or even!\n"))

val = "Even" if (num % 2 == 0) else val = "Odd"

print("The number you input is", val)
Remove the second 'val ='. What comes after the else is just assigned to val if the condition is not true.