Python Forum
Returning a Boolean operator and a variable both? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Returning a Boolean operator and a variable both? (/thread-6158.html)



Returning a Boolean operator and a variable both? - PythonAndArduino - Nov-08-2017

I'm very new to coding. I'm building a number guessing game. I had it working as a while loop, but I'm learning to define functions. I've built this, and I can make the results work if I have the resulting success or failure just print within my defined function, but I want it to return values outside of my defined function so I can act with the out there.

import random
import pygame
import sys

from pygame.locals import *

def game (theNumber, newNumber):
    try:
        int(newNumber)
    except:
        print("That's not going to work. I need a number from 0 to 10.")
        return

    myNumber = int(newNumber)
    if myNumber >= 11 or myNumber < 0:
        print("I said 0 to 10. Let's try again:")
        return
    elif theNumber == myNumber:
        return True, theNumber
    else:
        return False, theNumber
    
doYou = input("Would you like to play? ")
if doYou == "Yes":
    theNumber = random.randrange(0,10)
    newNumber = input("Guess a number from 0 to 10.")
    game(theNumber, newNumber)
else:
    print("Consent is vital, I need a clear answer: Yes")

if game(theNumber, newNumber) == True:
    print("Nope, the answer was", theNumber)
elif game(theNumber, newNumber) == False:
    print("Congratulations, you guessed it: ", theNumber)
I know I have unnecessary imports at the top, but I want to be able to act with the returns from my game function.


RE: Returning a Boolean operator and a variable both? - ineedastupidusername - Nov-08-2017

def myfunc():
    return False,17
a=myfunc()
print a[0]
print a[1]



RE: Returning a Boolean operator and a variable both? - PythonAndArduino - Nov-08-2017

Ah, so I need to define another variable as the returns of the functions. Thank you very much!