Ex 36 - making own game but stuck on how to return a value

I’ve started making my own small game similar to the bear game in ex 35.

It’s basic and based of the old paper/rock/scissor game.

The palyer has to walk through 3 different worlds and fight 3 villains by the paper/rock/scissor method.
If he succeeds he is send back to a cross-road where he can choose to go to another world and fight another opponent.

My problem is I want to register if a villain is dead, so when the player goes into a world where the villain is dead there’ll be another prompt.

I thought this would be straight forward using a return to set a villain_dead = True, however I can’t figure out how make a return from a nested if statement within a function.

Is this possible at all?
Or am I going around this in the wrong way?

I hope someone can help.

from sys import exit
import random

def ressurect(togo):
    if togo > 1:
        print(f"""Welcome back to life.
        You now have {togo} lives left""")

    else:
        print(f"""Welcome back to life.
        You now have {togo} live left - it's now or never or you'll forever stay trapped.""")

def where_to_go():
    print("You're now at a crossroad and there are 3 signs:\n1) Go left to the 'Valley of blunt scissors'\n2) Continue straight to the 'Canyon of crumbled paper'\n3) Right to the 'Mountain of broken rocks'")

    choice_count=0

    while True:
        choice = input('Where do you want to go?\n >')

        if choice == '1':
            print("You're on the way to the 'Valley of blunt scissors'\nI hope you brought some grit.")
            input()
            scissor()

        elif choice == '2':
            print("You're on the way to the 'Canyon of crumbled paper' - I hope you a sharp.")

        elif choice == '3':
            print("You're on the way to the 'Mountain of broken rocks' - I hope you are all wrapped up.")

        elif choice_count == 0 and (choice != '1' or choice != '2' or choice != '3'):
            print("I got no idea what means, try again.")
            choice_count += 1

        elif choice_count == 1 and (choice != '1' or choice != '2' or choice != '3'):
            print(f"For fuck sake {name}, learn to count to 3.")
            print("""Last warning!
            get it wrong again and you'll be dead""")
            choice_count += 1

        elif choice_count == 2 and (choice != '1' or choice != '2' or choice != '3'):
            exit()


def scissor(dead):
    print("As you walk into the valley, you pass alot of cut-up paper.")
    print("It looks like the office malculator had a Christmas party.")

    print(dead)


    if dead == False:

        print("Out of nowhere jumps a huge scissor.\nIt looks fucked up and blunt as hell but still big enough to cut you in two.")
        print("\nWhat do you want to do?\n1) Stay and fight for your life?\n2) Look what's in your backpack?\n3) Get the fuck out?")

        choice = input("> \n")

        if choice == "1":
            print('lets gamble')
            dead = game('Fucked up scissor')

        elif choice == "2":
            print('your backpack is currently: ')
        elif choice == "3":
            print('Fair enough!')
            where_to_go()
    else:
        print("The fucked up scissor is allready dead")
        print("Nothing more to come for, just go back to where you came from")
        where_to_go()



#This is the main structure of the game - which is linked to the 'rules' of the game.

def game(opponent):
    villain_choice = ['Scissor', 'Rock', 'Paper']
    my_options = ['1) Scissor', '2) Rock', '3) Paper']
    my_choice = ['Scissor', 'Rock', 'Paper']
    round_current = 0
    win_current = 0
    lost_current = 0
    opponent_name = opponent

    print('Lets go. Best out of 3.')

    while win_current < 3 > lost_current:
        print('\nYour options are:')
        #make a for loop to print all the options from my_options list
        for x in my_options:
            print(x)

        print("What's your move?")
        choice = input('> ')

        if choice == "1":
            #randomiseds opponents move with random.choice() from the villain_choice list
            villain_move = random.choice(villain_choice)
            #shows what I chose and pics it out of my_choice list - the first option in a list is always 0
            print("\nYou chose:", my_choice[0],"\nVillain chooses: ", villain_move )

            #Makes 3 variables that is filled in from a return in game_rules.
            win, round, lost = game_rules(my_choice[0], villain_move)

            #Adds a 1 to every win a 1 to every lost and a 1 to every round.
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")

        elif choice == "2":
            villain_move = random.choice(villain_choice)
            print("You chose", my_choice[1] )
            print("Villain chooses: '", villain_move,"'")

            win, round, lost = game_rules(my_choice[1], villain_move)
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")
        elif choice == "3":
            villain_move = random.choice(villain_choice)
            print("You chose", my_choice[2] )
            print("Villain chooses: '", villain_move,"'")

            win, round, lost = game_rules(my_choice[1], villain_move)
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")
        else:
            print("Not sure what that means")

    #print('loop finised')
    if win_current>2:
        print(f"You lucky bastard you pulled it off!\nThe {opponent_name} is dead")
        print("Lets fo back to the cross road")
        dead = True
        where_to_go(opponent_name, dead)


    elif lost_current>2:
        print(f"You're donzo.\nThe {opponent_name} fucked you up")
        print("You'll have to start over!")
    else:
        print("Something is wrong here")




# This is the underlaying game rules that explain what beats what.
# It returns a won round, lost lost round and
def game_rules(me, villain):
    if me == villain:
        print(f"Close call - you both chose {me}")
        print("It's an even")
        #Returns the a 0 win, +1 round and 0 lost because the game is even
        return 0, 1, 0 #win, round, lost

    elif me == 'Scissor' and villain == 'Paper':
        print("You win")
        return 1, 1, 0

    elif me == 'Scissor' and villain == 'Rock':
        print("You loose")
        return 0, 1, 1

    elif me == 'Rock' and villain == 'Scissor':
        print("You win")
        return 1, 1, 0

    elif me == 'Rock' and villain == 'Paper':
        print("You loose")
        return 0, 1, 1

    elif me == 'Paper' and villain == 'Rock':
        print("You win")
        return 1, 1, 0

    elif me == 'Paper' and villain == 'Scissor':
        print("You loose")
        return 0, 1, 1

    else:
        print('Something is wrong')


print("You're trapped inside the brain of 12 year old girl, the only way out is to go around her brain find a way out.")
name = input("What's your name? \n > ")

scissor_dead = False
life = 3

hello @ktrager,
I modified your program according to what you wanted.
I liked your game very much but you had and have a few mistakes that I tried to correct.
Even i was stuck at the same problem for my game.
here is a link about my query


The solution that I discovered might not be what experienced developers use (hence I did not delete it)

from sys import exit
import random

opponent_name = ()
scissor_alive = True
rock_alive = True
paper_alive = True

def ressurect(togo):
    if togo > 1:
        print(f"""Welcome back to life.
        You now have {togo} lives left""")

    else:
        print(f"""Welcome back to life.
        You now have {togo} live left - it's now or never or you'll forever stay trapped.""")
    where_to_go() # to start the game 

def where_to_go():
    print("You're now at a crossroad and there are 3 signs:\n1) Go left to the 'Valley of blunt scissors'\n2) Continue straight to the 'Canyon of crumbled paper'\n3) Right to the 'Mountain of broken rocks'")

    choice_count=0

    while True:
        choice = int(input('Where do you want to go?\n >'))

        if choice == 1:
            if scissor_alive == True:
                print("You're on the way to the 'Valley of blunt scissors'\nI hope you brought some grit.")
                input()
                scissor() # the argument must be provided in the parentheses
                # I removed the 'dead' argument because in my opinion if did'nt have any use
            elif scissor_alive == False:
                print("already chosen once and is dead!")

        elif choice == 2:
            print("You're on the way to the 'Canyon of crumbled paper' - I hope you a sharp.")

        elif choice == 3:
            print("You're on the way to the 'Mountain of broken rocks' - I hope you are all wrapped up.")

        elif choice_count == 0 and (choice != 1 or choice != 2 or choice != 3):
            print("I got no idea what means, try again.")
            choice_count += 1

        elif choice_count == 1 and (choice != 1 or choice != 2 or choice != 3):
            print(f"For fuck sake {name}, learn to count to 3.")
            print("""Last warning!
            get it wrong again and you'll be dead""")
            choice_count += 1

        elif choice_count == 2 and (choice != 1 or choice != 2 or choice != 3):
            exit()


def scissor():
    print("As you walk into the valley, you pass alot of cut-up paper.")
    print("It looks like the office malculator had a Christmas party.")
    dead = 0
    print(dead)


    if dead == False:

        print("Out of nowhere jumps a huge scissor.\nIt looks fucked up and blunt as hell but still big enough to cut you in two.")
        print("\nWhat do you want to do?\n1) Stay and fight for your life?\n2) Look what's in your backpack?\n3) Get the fuck out?")

        choice = input("> \n")

        if choice == "1":
            print('lets gamble')
            game('Fucked up scissor')

        elif choice == "2":
            print('your backpack is currently: ')
        elif choice == "3":
            print('Fair enough!')
            where_to_go()
    else:
        print("The fucked up scissor is allready dead")
        print("Nothing more to come for, just go back to where you came from")
        where_to_go()



#This is the main structure of the game - which is linked to the 'rules' of the game.

def game(opponent):
    villain_choice = ['Scissor', 'Rock', 'Paper']
    my_options = ['1) Scissor', '2) Rock', '3) Paper']
    my_choice = ['Scissor', 'Rock', 'Paper']
    round_current = 0
    win_current = 0
    lost_current = 0
    
    global opponent_name
    opponent_name = opponent

    print('Lets go. Best out of 3.')

    while win_current < 3 > lost_current:
        print('\nYour options are:')
        #make a for loop to print all the options from my_options list
        for x in my_options:
            print(x)

        print("What's your move?")
        choice = input('> ')

        if choice == "1":
            #randomiseds opponents move with random.choice() from the villain_choice list
            villain_move = random.choice(villain_choice)
            #shows what I chose and pics it out of my_choice list - the first option in a list is always 0
            print("\nYou chose:", my_choice[0],"\nVillain chooses: ", villain_move )

            #Makes 3 variables that is filled in from a return in game_rules.
            win, round, lost = game_rules(my_choice[0], villain_move)

            #Adds a 1 to every win a 1 to every lost and a 1 to every round.
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")

        elif choice == "2":
            villain_move = random.choice(villain_choice)
            print("You chose", my_choice[1] )
            print(f"'{opponent}chooses: '", villain_move,"'")

            win, round, lost = game_rules(my_choice[1], villain_move)
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")
        elif choice == "3":
            villain_move = random.choice(villain_choice)
            print("You chose", my_choice[2] )
            print("Villain chooses: '", villain_move,"'")

            win, round, lost = game_rules(my_choice[1], villain_move)
            win_current += win
            round_current += round
            lost_current += lost
            print(f"You've won: {win_current}\nLost: {lost_current}\nRound: {round_current}")
        else:
            print("Not sure what that means")

    #print('loop finised')
    global scissor_alive, rock_alive, paper_alive
    if win_current>2:
        print(f"You lucky bastard you pulled it off!\nThe {opponent_name} is dead")
        print("Lets fo back to the cross road")
        if opponent_name =='Fucked up scissor':
            scissor_alive = False
        elif opponent_name == 'name of rock':
            rock_alive = False
        elif opponent_name == 'name of paper':
            paper_alive = False 
        where_to_go() # (opponent_name, dead)


    elif lost_current>2:
        print(f"You're donzo.\nThe {opponent_name} fucked you up")
        print("You'll have to start over!")
    else:
        print("Something is wrong here")




# This is the underlaying game rules that explain what beats what.
# It returns a won round, lost lost round and
def game_rules(me, villain):
    if me == villain:
        print(f"Close call - you both chose {me}")
        print("It's an even")
        #Returns the a 0 win, +1 round and 0 lost because the game is even
        return 0, 1, 0 #win, round, lost

    elif me == 'Scissor' and villain == 'Paper':
        print("You win")
        return 1, 1, 0

    elif me == 'Scissor' and villain == 'Rock':
        print("You loose")
        return 0, 1, 1

    elif me == 'Rock' and villain == 'Scissor':
        print("You win")
        return 1, 1, 0

    elif me == 'Rock' and villain == 'Paper':
        print("You loose")
        return 0, 1, 1

    elif me == 'Paper' and villain == 'Rock':
        print("You win")
        return 1, 1, 0

    elif me == 'Paper' and villain == 'Scissor':
        print("You loose")
        return 0, 1, 1

    else:
        print('Something is wrong')


print("You're trapped inside the brain of 12 year old girl, the only way out is to go around her brain find a way out.")
name = input("What's your name? \n > ")

scissor_dead = False
life = 3

ressurect(life)

YOU WILL NEED TO CHANGE A LOT OF LINES IN IT THAT I HAVE MODIFIED FOR MY USE.

Hope it helps.

by Dev

image

it has a problem that even though the system and i chose different options, it said close call both of you chose rock! :frowning:

Hi @dvinci35, could you please edit your posts and wrap your code in code tags:

[code]
# your code
[/code]

I’d be happy to take a look, but right now it’s a bit of a visual mess. :slight_smile:

I fixed your code for you. Read what @florian said about posting code so people can read it.

Now, I think what you’re misssing the global keyword. Any variable you want to share between functions, you have to:

  1. create at the top of the file
  2. at the top of each function put: global VAR
  3. Then any changes to that variable in one function will happen in other functions.

For example:

DEAD=False

def fight():
    global DEAD
    DEAD=True

def isDead():
    global DEAD
    if DEAD: print("you died")
1 Like