Python Forum
Trouble with assigning a string value in conditional statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Trouble with assigning a string value in conditional statement (/thread-10987.html)



Trouble with assigning a string value in conditional statement - juliabrushett - Jun-16-2018

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)



RE: Trouble with assigning a string value in conditional statement - ichabod801 - Jun-16-2018

Remove the second 'val ='. What comes after the else is just assigned to val if the condition is not true.