Python Forum
Trouble with assigning a string value in conditional statement
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with assigning a string value in conditional statement
#1
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)
Reply
#2
Remove the second 'val ='. What comes after the else is just assigned to val if the condition is not true.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble converting a string into an int DreDigital 2 1,614 Jan-29-2020, 09:41 PM
Last Post: DreDigital
  format String in SQL statement. Steven 9 6,558 May-29-2017, 08:41 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020