Python Forum
Returning a Boolean operator and a variable both?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning a Boolean operator and a variable both?
#1
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.
"Nobody really knows how to program, the pros Google too." -A Google Engineer
Reply
#2
def myfunc():
    return False,17
a=myfunc()
print a[0]
print a[1]
Reply
#3
Ah, so I need to define another variable as the returns of the functions. Thank you very much!
"Nobody really knows how to program, the pros Google too." -A Google Engineer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AND Boolean operator syntax error jsb83 3 2,758 May-31-2019, 08:34 AM
Last Post: perfringo
  Boolean: if variable is capitalized Truman 9 15,910 Jan-09-2018, 11:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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