Bottom Page

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help with functions
I am coding a text based game on python for a school project and i would like the player to have the ability to return to the start if they wish to.

#beginning text#
import random
import time
print("This is the game of stuff, you have to get 6 treasure to win. You might get killed on the way so stay safe.")
score = 0
lives = 6
c1 = 0 

print(" Your current score = ", score)
print(" You currently have ", lives, "lives remaining")

#castle choice#
def start(castle):
  print("You can choose a possibility of castles to enter, but you will have to enter them all\n You can either enter the fire castle , ice castle or earth castle\n Enter with your own risk")
print("Fire = 1")
print("Ice = 2")
print("Earth = 3")
c1 = int(input("What castle would you like to enter first?"))
#room choice for fire castle#

if c1 == 1:
    print("Welcome to the fire castle. There is a possible 3 pieces of treasure hiding in here and it is your job to find them\n Only when you have received 2 of the three treasure pieces you can leave")
    print("Throne Room = 1")
    print("Guard Room = 2")
    print("Dining Room = 3")
    print("Leave the castle = 4") 
    r1 = int(input("What room would you like to visit?"))
    #fire throne room#
    if r1 == 1:
      print("Welcome to the Throne Room, you can see some unguarded treasure at the end of the room")
      print("Yes = 1")
      print("No = 2")
      y1 = int(input("Would you like to go and get the treasure?"))
      #fire throne room choice#
      if y1 == 1:
        print("you walk towards the treasure and nothing happens, you pick up the treasure and place it in your pocket")
        score = score + 1
        print("You now have ",score," pieces of treasure")
        print("You now leave the room")
      elif y1 == 2:
        print("You leave the room, potentially a wise decision but you may never know") 
        #guard room#
    elif r1 == 2:
      print("You walk into the room, the floor instantly collapses underneath you and you fall into the lava")
      lives = lives - 1
    elif r1 == 3:
      print("You enter the bedroom and see some treasure but it is guarded by a dragon")
      print("You can either attempt to bluff the dragon, try and kill the dragon or leave the room")
      print("Bluff = 1")
      print("Fight = 2")
      print("Run = 3")
      bfl = int(input("What would you like to do?"))
      #bedroom choice#
      if bfl == 1:
        print("The dragon sees you and speaks to you")
        print("DRAGON: 'Hello, you can have the treasure if you complete the riddle, else you will die")
        print("DRAGON: 'What get's wetter the more it dries'")
        print("Shower = 1")
        print("Towel = 2")
        print("Bath = 3")
        #bedroom dragon riddle#
        oo = int(input("What are you going to choose"))
        if oo == 1:
          print("DRAGON: Ha ha ha, idiot, now you die")
          print("The dragon bites your head of and you die")
          lives = lives - 1
        elif oo == 2:
          print("DRAGON: Correct, well done. You can now have the treasure")
          print("You put the treasure in your pocket")
          score = score + 1
        elif oo == 3:
          print("DRAGON:How can you be that stupid?")
          print("Dragon breathes fire and you burn to death, ouch")
          lives = lives - 1
          print("Error in knight's code, incorrect input value")
      if r1 == 4:
        print("ok, you may leave but you remember, you must return at some point") 
If you put your game logic in a function, then you can call that function repeatedly from a while loop.

For example:
>>> import random
>>> def play_game():
...   secret_number = random.randint(1, 10)
...   print("I'm thinking of a number from 1 to 9.  You have three guesses to get it.")
...   for guess in range(3):
...     attempt = input(f"Guess #{guess+1}: ")
...     try:
...       if int(attempt) == secret_number:
...         return True
...     except ValueError:
...       print("That's not a number :(")
...     else:
...       print("Incorrect")
...   return False
>>> running = True
>>> while running:
...   winner = play_game()
...   if winner:
...     print("You got it!")
...   else:
...     print("Sorry, you ran out of guesses.")
...   try_again = input("Play again? [yes/no] ")
...   running = try_again[0].lower() == "y"
I'm thinking of a number from 1 to 9.  You have three guesses to get it.
Guess #1: 2
Guess #2: 4
Guess #3: 9
Sorry, you ran out of guesses.
Play again? [yes/no] YES
I'm thinking of a number from 1 to 9.  You have three guesses to get it.
Guess #1: 1
Guess #2: 2
Guess #3: 3
Sorry, you ran out of guesses.
Play again? [yes/no] no

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Time multiple functions within functions Cortessizzz 4 388 Jan-09-2019, 04:15 PM
Last Post: Cortessizzz
  setting parameters for functions and nested functions mepyyeti 5 872 Feb-25-2018, 06:42 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)