1

So I'm trying to get an if statement to output a single string, but for some reason, it's outputting multiple? Trying something like 8 usually does this:

n = int(input('Enter a number: '))

W = print('Weird')
nW = print('Not Weird')

if (n % 2) == 0:
    if n == range(2, 5):
        nW
    elif n == range(6,20):
        W
    elif n > 20:
        nW
elif (n % 2) != 0:
    nW
else:
    print('Invalid Input')

Example input and output:

Enter a number: 8
Weird
Not Weird

I only want it to output one string, based on conditions met within the two if statements. Any idea what I'm missing? Thanks!

4
  • 3
    You seem to be under the misapprehension that: W = print('Weird') will not print. You need to go back to the documentation on print(). Commented Mar 29, 2020 at 20:51
  • 4
    You seem to have a misconception. From the way your code is written, it looks like you expect every instance of nW and W to invoke the respective calls to print which you 'bound' above. That's not how it works. What you're actually doing is printing two strings ("Weird" and "Not Weird"), and then binding the return value of print (which is None) to those variables. You'll have to use separate prints on the actual lines where you want printing to take place. You can't just bind entire executable Python snippets to a variable, and then expect it to work. Commented Mar 29, 2020 at 20:51
  • @user10987432 You could post that as an answer and I would upvote it Commented Mar 29, 2020 at 20:55
  • I'm voting to close this, I can't really see it being useful in the future. Commented Mar 30, 2020 at 0:36

2 Answers 2

3
W = print('Weird')
nW = print('Not Weird')

These lines will cause printing to happen immediately, and assign the value that print returns (the special value None) to each of W and nW.

if n == range(2, 5):
    nW

This (similarly for the other lines that just have W or nW by themselves) will not cause print to be called. They will check what the current value of nW (or W) is, and then ignore that value - in much the same way that if you wrote 1 + 2 on a line by itself in the middle of your program, it would compute 3 and then do nothing with that 3 result.

If you want to "alias" a function call like this, you have some options: you can define your own simpler function explicitly, for example

def W():
    print('Weird')

or you can use e.g. functools.partial from the standard library to "bind" parameters to the call:

import functools

W = functools.partial(print, 'Weird')
# Now W is a special object that represents calling `print` and passing `'Weird'` to it

But in any of these cases you will still need to call the function: W(), not W.

The more reasonable approach is to give names to the messages you want to print, and then print them at the appropriate times:

W = 'Weird'
# and later you can just
print(W)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this makes so much sense! I really didn't realize that print would do that, although a friend mentioned you couldn't assign print to a variable. Thank you so much again! I think I'm going to use ``` w = 'Weird' print(w) ```
1

The problem is that you're expecting W = print('Weird') to bind the expression print('Weird') to the name W, but that's not how it works. It actually calls print('Weird'), which prints Weird, then binds the return value, None, to W.

One way to fix it is define W and nW as strings, then print them.

W = 'Weird'
...
print(W)

Comments

Your Answer

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.