#๐Ÿ”’ i need help with return statements

45 messages ยท Page 1 of 1 (latest)

wet needle
tough mangoBOT
#

@wet needle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wheat yew
#

And what about return?

#

!return

tough mangoBOT
#
Return statement

A value created inside a function can't be used outside of it unless you return it.

Consider the following function:

def square(n):
    return n * n

If we wanted to store 5 squared in a variable called x, we would do:
x = square(5). x would now equal 25.

Common Mistakes

>>> def square(n):
...     n * n  # calculates then throws away, returns None
...
>>> x = square(5)
>>> print(x)
None
>>> def square(n):
...     print(n * n)  # calculates and prints, then throws away and returns None
...
>>> x = square(5)
25
>>> print(x)
None

Things to note

  • print() and return do not accomplish the same thing. print() will show the value, and then it will be gone.
  • A function will return None if it ends without a return statement.
  • When you want to print a value from a function, it's best to return the value and print the function call instead, like print(square(5)).
wet needle
#

like it gets to starting

#

and thats about it

#

when theres supposed to be game'

#

i think i wrote the retrurns wrong

#

wait

#

return doesnt work with strings?

wheat yew
#

return works with all objects

wet needle
#

right so this should return a 0 right?

#

(if the if statements not actived)

wheat yew
#

movevalid is never reassigned in computerbasic.

wet needle
#

o

#

thank u bro ๐Ÿ’€

wheat yew
#

When code is getting stuck, put prints in the suspect loops and see what's printing infintely.

wet needle
#

alright

#

appreciate it

wooden pike
#

ckwins() returns "", "computer", or "player" and you are testing for true false

wheat yew
wet needle
#

got that fixed

#

for some reason the loop does't switch to 'ended' after someone wins

wheat yew
#

Have you verified that winnermain is what you think it is in the main loop?

wet needle
#

let me check

#

yeah

#

for some reason its updates at the end tho

#

the 0 = playing 1 = not

wheat yew
#

This looks suspect:

print(winner + " wins!")
winner = gamevalue
#

You print winner, then overwrite winner with gamevalue. Print out gamevalue too and see what it is, since that's what is returned.

#

Actually, nvm, I don't think that's it.

wet needle
#

oh wait u have old version

wheat yew
#

I'm getting off now. Hopefully someone else will be able to help.

#

Also:

#

!code

tough mangoBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

wet needle
#

alr

#

thanks for the help

#

!close

tough mangoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.