0

I am very new to Python (and using Python 3). Apologies---I know I must be making a very basic mistake.

Here is the structure of the mistake and then I'll give an example: I wrote a function func(x). To test that it does the "right" things, I plugged in value x1, i.e. print(func(x1)). The output was y1 which was correct. Then I tried print(func(x2)). That again gave me y1 and not the correct y2. And similarly with other values of x. At first I thought it was a mistake with that specific function, but then I had similar issues with other functions. Am I making a mistake common to all the functions?

Below is one example; I've tried this (and other functions) with several different variations and still had the problem. I can give other examples, if that would help.

def num_to_day(x):
 if 0:
 return "Sunday"
 elif 1:
 return "Monday"
 elif 2:
 return "Tuesday"
 elif 3:
 return "Wednesday"
 elif 4:
 return "Thursday"
 elif 5:
 return "Friday"
 elif 6:
 return "Saturday"
 else:
 return "Not Valid"
print(num_to_day(5))
Tonechas
13.8k16 gold badges52 silver badges85 bronze badges
asked Jul 12, 2017 at 16:48
1
  • 4
    You have to test the condition in you if, like this if x == 0: .. elif x == 1 ... Commented Jul 12, 2017 at 16:50

3 Answers 3

4

With your if statements, you need to specify x equals each number--for example, if x == 0:.

Currently, you're testing the truth value of a number itself, with no relation to what x you have specified. To see this, try:

if 1:
 print('Monday')
Monday

You might find it useful to know that in Python, 0 evaluates to False while other integers return True:

print([bool(num) for num in range(7)])
[False, True, True, True, True, True, True]

bool tests the truth value of its argument.

Lastly, one alternative of many would be to lookup your weekday from a dict. .get allows you to specify a value that gets returned if an error is thrown.

def num_to_day(x):
 days = {
 0 : 'Sunday',
 1 : 'Monday',
 2 : 'Tuesday',
 3 : 'Wednesday',
 4 : 'Thursday',
 5 : 'Friday',
 6 : 'Saturday'
 }
 return days.get(x, 'Not Valid')
num_to_day(0)
Out[54]: 'Sunday'
num_to_day(10)
Out[55]: 'Not Valid'
answered Jul 12, 2017 at 16:50
Sign up to request clarification or add additional context in comments.

3 Comments

That worked! Thanks! (It won't let me accept the answer for 12 minutes)
You also might want to use lists for this comparison. ex. ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][day_num]
yes, i had tried various version ... but i thought the posted one was easiest to figure out the mistake i was making... i knew it must be basic...
2

You're not using your argument inside your function at all.

def num_to_day(x):
 if x == 0: # <-- Now x gets evaluated
 return "Sunday"
 elif x == 1:
 return "Monday"
 elif x == 2:
 return "Tuesday"
 elif x == 3:
 return "Wednesday"
 elif x == 4:
 return "Thursday"
 elif x == 5:
 return "Friday"
 elif x == 6:
 return "Saturday"
 else:
 return "Not Valid"
print(num_to_day(5))
answered Jul 12, 2017 at 16:52

Comments

0

You're passing an argument to your function and then not using it at all. To make if work, you must actually compare x to all those numbers, simple if 0: is equivalent to if False:, not to if x == 0:.

And anyway, this cascade of elifs is a prime example of how not to code. A much better solution would be to use a list or a dictionary:

def num_to_day(x):
 days_of_week = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
 return days_of_week[x]
answered Jul 12, 2017 at 16:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.