Beginner's work

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Beginner's work

Postby Ringer » Fri Oct 18, 2013 7:59 pm

Hello people! New guy here.
I recently started learning Python (won't bore you with my story) and decided to make a little game. It's quite simple, based on a question - answer logic. I was hoping to receive some feedback from experienced programmers in order to make better code in the future.

Code: Select all
from sys import exit

print "This is a simple game. You only have to write your choice. It's up to you to survive or die trying. You can type in \"stats\" at any time in order to get your current stats printed out on the screen. Have fun!"
def wilderness():

   energy = 50
   food = 0
   
   if energy <= 0:
      dead()
   
   print "You wake up and find yourself in the wilderness. You have two choices:"
   while True:
      print "1. Go towards the lake to the right"
      print "2. Go in the forest to the left"
      
      answer = raw_input ("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "forest" in answer:
         energy -= 5
         forest(energy, food)
      elif "lake" in answer:
         energy -= 5
         lake(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def lake (en, fd):

   energy = en
   food = fd
   
   if energy <= 0:
      dead()
   
   print "You arrived at a lake. What do you want to do?"
   while True:
      print "1. Find shelter"
      print "2. Search for food"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "shelter" in answer:      
         
         energy -= 10
         while True:
            print "Do you want to sleep in the fisher's tent or under a tree?"
            answer = raw_input("> ")
            
            while "stats" == answer:
               print "energy = %d\n" % energy
               answer = raw_input("> ")
            
            if "tent" in answer:
               sleep(energy, food)
            elif "tree" in answer:
               dead()
            else:
               print "There's no time to waste. Choose fast!"
               
      elif "food" in answer:
         search_food(energy, food)
         
      else:
         print "There's no time to waste. Choose fast!"

def search_food (en, fd):

   energy = en
   food = fd
   
   if energy <= 0:
      dead()
   
   while True:
      print "Do you want to fish or to get some berries?"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "fish" in answer:
         energy -= 10
         food = 1
         fish(energy, food)
      elif "berries" in answer:
         dead()
      else:
         print "There's no time to waste. Choose fast!"

def fish(en, fd):

   energy = en
   k = 0
   while (energy < 50) and (k < 15):
      energy += 1
      k += 1
   food = fd
   
   if energy <=0:
      dead()
   
   print "Now that you found food you have two choices"
   while True:
      print "1. Find shelter"
      print "2. Go forward"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "shelter" in answer:
         sleep(energy, food)
      elif "forward" in answer:
         forward(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def sleep (en, fd):

   energy = en
   food = fd
   k = 0
   while (energy < 50) and (k < 10):
      energy += 1
      k += 1
   
   if energy <=0:
      dead()
      
   print "After waking up, you find yoursel with two choices at hand"
   while True:
      print "1. Search for food"
      print "2. Go forward"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "food" in answer:
         search_food(energy, food)
      elif "forward" in answer:
         forward(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def forward(en, fd):
   
   food = fd
   energy = en
   
   if energy <=0:
      dead()
   
   print "While going forward you find two things to do"
   while True:
      print "1. Get some food"
      print "2. Find some water"
      
      answer = raw_input ("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "food" in answer:
         search_food(energy, food)
      elif "water" in answer:
         water(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def water(en, fd):
   
   food = fd
   energy = en - 5
   
   if energy <=0:
      dead()
   
   while True:
      print" You can drink water from the lake or from the river"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "lake" in answer:
         lake2(energy, food)
      elif "river" in answer:
         river(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def lake2 (en, fd):

   energy = en - 5
   food = fd
   
   if energy <=0:
      dead()
   
   print "The water from the lake was no good, but you still have a couple of choices left"
   while True:
      print "1. Drink from the lake again"
      print "2. Search for water again"
      print "3. Go forward"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if ("lake" in answer) or ("drink" in answer) or ("Drink" in answer):
         lake(energy, food)
      elif ("water" in answer) or ("Search" in answer) or ("search" in answer):
         water(energy, food)
      elif "forward" in answer:
         forward2(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def river (en, fd):
   
   energy = en
   food = fd
   k = 0
   if (energy < 100) and (food == 1):
      while (energy < 100) and (k < 10):
         energy += 1
         k += 1
   elif energy < 50:
      while (energy < 50) and (k < 10):
         energy += 1
         k += 1
   
   if energy <=0:
      dead()
   
   print "You just arrived to a river. The water is refreshing and gives you new energies, but you still have to go forward"
   while True:
      print "1. Drink from the river once more"
      print "2. Go forward"
   
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
   
      if "drink" in answer:
         river(energy, food)
      elif "forward" in answer:
         forward2(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def forest(en, fd):
   
   energy = en
   food = fd
   
   if energy <=0:
      dead()
   
   print "Arriving into the forest, you have two choices"
   while True:
      print "1. Look for shelter"
      print "2. Look for food"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "shelter" in answer:
         search_shelter(energy, food)
      elif "food" in answer:
         search_food2(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def search_food2(en, fd):
   
   energy = en
   food = fd
   
   if energy <=0:
      dead()
   
   print "What do you want to eat?"
   while True:
      print" 1. Berries"
      print "2. Hunt something"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "berries" in answer:
         dead()
      elif "hunt" in answer:
         fail(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def fail(en, fd):
   
   energy = en - 15
   food = fd
   
   if energy <=0:
      dead()
   
   print "You failed to find any food. What do you want to do next?"
   while True:
      print "1. Find a shelter"
      print "2. Wait till morning"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "shelter" in answer:
         search_shelter(energy, food)
      elif ("morning" in answer) or ("wait") in answer:
         morning_comes(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def search_shelter(en, fd):
   
   energy = en - 5
   food = fd
   
   if energy <=0:
      dead()
   
   print "Where are you going to sleep?"
   while True:
      print "1. Bear's cave"
      print "2. Abbandoned treehouse"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "bear" in answer:
         dead()
      elif "treehouse" in answer:
         treehouse(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def treehouse(en, fd):
   
   energy = en
   k = 0
   if energy < 100:
      while (energy < 100) and (k < 5):
         energy += 1
         k += 1
   food = fd
   
   if energy <=0:
      dead()
   
   morning_comes(energy, food)

def morning_comes(en, fd):
   
   energy = en
   food = fd
   
   if energy <=0:
      dead()
   
   print "It's morning. What are you going to do"
   while True:
      print "1. Look for food"
      print "2. Look for water"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "food" in answer:
         search_food3(energy, food)
      elif "water" in answer:
         water(energy, food)
      else:
         print "There's no time to waste. Choose fast!"

def search_food3(en, fd):
   
   energy = en - 15
   food = fd
   
   if energy <=0:
      dead()
   
   print "What do you want to eat?"
   while True:
      print "1. Hunt something"
      print "2. Berries"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "hunt" in answer:
         failure2(energy, food)
      elif "berries" in answer:
         dead()
      else:
         print"There's no time to waste. Choose fast!"

def failure2(en, fd):
   
   energy = en - 15
   food = fd
   
   if energy <=0:
      dead()
   
   print "You failed to hunt for food. What are you going to do next?"
   while True:
      print "1. Go forward"
      print "2. Look for water"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "forward" in answer:
         forward2(energy, food)
      elif "water" in answer:
         water(energy, food)
      else:
         print "There's no time to waste. Choose fast!"
   
def forward2(en, fd):
   
   energy = en
   food = fd
   money = 0
   
   if energy <=0:
      dead()
   
   print "You decided to go forward. What will you do now?"
   while True:
      print "1. Make some traps"
      print "2. Go along the river"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\n" % energy
         answer = raw_input("> ")
      
      if "traps" in answer:
         nothing(energy, food)
      elif "river" in answer:
         village(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def nothing(en, fd):
   
   energy = en - 15
   food = fd
   
   if energy <=0:
      dead()
   
   print "You made the traps but did nothing with them. What a waste of time."
   
   forward2(energy, food)

def village(en, mn):
   
   energy = en - 10
   money = mn
   
   if energy <=0:
      dead()
   
   print "You arrived to a small village. What are you going to do?"
   while True:
      print "1. Go to the town hall"
      print "2. Go to the inn"
      
      answer = raw_input ("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "inn" in answer:
         inn(energy, money)
      elif "town hall" in answer:
         town_hall(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def town_hall(en, mn):
   
   energy = en - 5
   money = mon
   
   if energy <=0:
      dead()
   
   print "You arrived at the town hall. What next?"
   while True:
      print "1. Find some work"
      print "2. Beg for money"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "work" in answer:
         fail(energy, money)
      elif "beg" in answer:
         beg(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def fail(en, mn):
   
   energy = en - 5
   money = mn
   
   if energy <=0:
      dead()
   
   print "You failed to find yourself a job and go back to the village."
   
   village(energy)

def beg(en, mn):
   
   energy = en -20
   money = mn + 65
   
   if energy <=0:
      dead()
   
   print "You succesfully got some cash and returned to the village."
   
   village(energy, money)

def inn(en, mn):
   
   energy = en - 5
   money = mn
   
   if energy <=0:
      dead()
   
   print "You arrived to the inn. What are you going to do next?"
   while True:
      print "1. Get into a fight"
      print "2. Rent a room"
      print "3. Buy yourself some food"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "fight" in answer:
         fight(energy, money)
      elif ("room" in answer) and (money >= 5):
         room(energy, money)
      elif ("food" in answer) and (money >= 20):
         eat(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def fight (en, mn):
   
   energy = en - 50
   money = mn + 200
   
   if energy <=0:
      dead()
   
   print "You got into a fight, recieved a beating and someone's fat purse. What next?"
   while True:
      print "1. Go back into the inn"
      print "2. Go forward"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "inn" in answer:
         inn(energy, money)
      elif "forward" in answer:
         castle(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def room(en, mn):
   
   energy = en
   k = 0
   if energy < 100:
      while (energy < 100) and (k < 15):
         energy += 1
         k += 1
   money = mn - 5
   
   if energy <=0:
      dead()
   
   print "You slept in a nice and comfortable bed. What next?"
   while True:
      print "1. Go back into the inn"
      print "2. Go forward"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "inn" in answer:
         inn(energy, money)
      elif "forward" in answer:
         castle(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def eat(en, mn):
   
   energy = en
   k = 0
   if energy < 100:
      while (energy < 100) and (k < 20):
         energy += 1
         k += 1
   money = mn - 20
   
   if energy <=0:
      dead()
   
   print "You had a decent meal. What next?"
   while True:
      print "1. Go forward"
      print "2. Get back to the inn"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "forward" in answer:
         castle(energy, money)
      elif "inn" in answer:
         inn(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def castle(en, mn):
   
   energy = en - 30
   money = mn
   fancy = 0
   
   if energy <=0:
      dead()
   
   print "You arrived to the castle. What next?"
   while True:
      print "1. Go to the king"
      print "2. Get yourself something to eat"
      
      answer = raw_input("> ")
      
      while "stats" == answer:
         print "energy = %d\nmoney = %d" % (energy, money)
         answer = raw_input("> ")
      
      if "king" in answer:
         king(energy, money, fancy)
      elif (("food" in answer) or ("eat" in answer))and (money >= 110):
         fancy2(energy, money)
      else:
         print "There's no time to waste. Choose fast!"

def fancy2 (en, mn):
   
   energy = en
   k = 0
   if energy < 100:
      while (energy < 100) and (k < 30):
         energy += 1
         k += 1
   money = mn - 110
   fancy = 1
   
   if energy <=0:
      dead()
   
   print "After eating a proper meal, you got yourself some fancy clothes. Next destination: the king!"
   
   king(energy, fancy)

def king(en, fc):
   
   energy = en
   fancy = fc
   
   if energy <=0:
      dead()
   
   if fancy == 0:
      print "The king won't talk to a beggar like you! After getting beaten up, you manage to get back into the castle."
      castle(en, mn)
   else:
      print "The king will talk to you now!"
      kings_room()

def kings_room():
      
      print "You got to the king. He is very displeased with your presence. What are you going to do?"
      while True:
         print "1. Assault him"
         print "2. Beg for mercy"
         
         answer = raw_input("> ")
         
         if "assault" in answer:
            win()
         elif "beg" in answer:
            prison()
         else:
            print "There's no time to waste. Choose fast!"

def win():
   
   print "After assaulting the king, your memory came back to you and you realised that actually you are the true king. Congratulations! You won!"
   while True:
      print "Do you want to play again? Y/N"
      
      answer = raw_input("> ")
      
      if "Y" == answer:
         wilderness()
      elif "N" == answer:
         exit(0)
      else:
         print "That's no vaild answer!"

def prison():
   
   print "It looks like it wasn't such a good idea to beg for mercy. The king threw you into the prison. After a couple of days you woke up in the same place you started all of this."
   wilderness()

def dead():
   
   while True:
      print "You are dead. want to start over? Y/N"
      
      answer = raw_input("> ")
      
      if "Y" == answer:
         wilderness()
      elif "N" == answer:
         exit(0)
      else:
         print "That's no vaild answer!"

print wilderness()


Thanks for expressing your oppinion!
Last edited by stranac on Fri Oct 18, 2013 8:15 pm, edited 1 time in total.
Reason: First post lock
User avatar
Ringer
 
Posts: 2
Joined: Fri Oct 18, 2013 7:36 pm

Re: Beginner's work

Postby stranac » Fri Oct 18, 2013 8:19 pm

Oh wow, that's a lot of code. Sorry, but I won't be reading all of it.
Here's something I noticed in the first ten lines of code:
Code: Select all
       energy = 50
       food = 0
       
       if energy <= 0:
          dead()

You're setting energy to 50 and immediately check if it's 0 or less. That if block will never be executed.

Anyway, welcome to the forum.
It's always nice to see new people, especially when they post with code tags AND in the correct area.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Beginner's work

Postby micseydel » Fri Oct 18, 2013 10:06 pm

I'm not going to read it all either, but here's something I noticed
Code: Select all
def lake (en, fd):

   energy = en
   food = fd

   # ...

Just do
Code: Select all
def lake(energy, food):
    # ...

and if you need to modify the value,
Code: Select all
def lake(energy, food):
    energy -= 10
    # ...

More importantly though... the structure of your program is unusual. Typically, a high-level function does something, returns, and then you use that return value in subsequent lines of the program. Instead, in your program, you never let functions return, but instead just keep calling the next function. What I like to do with my programs is have a main() function that has some high level functions that I call one after another, so that a reader of the program at a high level knows what's going to happen. In your program, if I want to know how to get to a specific place, I have to look at where it's call, then where that's call, then where that's called... you just seem to have developed bad habits writing a large program before you understood things well (not meant as criticism as much as feedback; after all, you had the initiative to do this whole thing).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1203
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron