Advertisement

I'm just getting started with python and have run into a bit of trouble

Started by September 24, 2017 06:38 AM
17 comments, last by WinterDragon 7 years ago
10 hours ago, WinterDragon said:

it (the program) focuses alot on indentation, so I've been fixing that alot.

It does indeed, I don't know how your editor reacts on TABs, but if they don't automagically expand to spaces, getting indentation right is complicated. Find a better editor if this is the case.

 

10 hours ago, WinterDragon said:

man I miss line numbers.

I don't know what text editor you use, but if the current one doesn't work for you, find a different one. I am not using Windows, but I heard Notepad++ to be nice, no doubt there are others too.

 

10 hours ago, WinterDragon said:

I may need to go back to the book about the correct way to use while loops and define functions/methods?

These things are independent of each other. A while loop repeats its inner body until the condition at the top becomes False, or you execute the "break" statement in that body.

A function definition gives a sequence of statements (a body) a name, so you can call (use) it from anywhere by saying the same name again. You leave a function body either by reaching the end of the body or by executing the "return" statement.

The problem of how to divide your code between different functions is not always clear, but there are general guide lines. A function definition should not be too long (a number is hard to give, but let's say 50-70 lines is usually a good maximum), and it should be mostly independent (so you don't have a zillion variables going in through the parameter list, and a zillion out through the return statement).

Functions work best if you need the same code at several places. You can then simply call that code by the name of the function instead of doing a verbatim copy of the code (which is pretty much deadly if you ever need to change the code).

10 hours ago, WinterDragon said:

the error I'm struggling with is it doesn't print anything on the screen when the program starts.

Maybe you only have "def" things, and no main code? ie


def myfunc():
  print("Hello!")
  other_func("world")
 
def other_func(w):
  print(w)

While this looks complete, there is no "myfunc()" call at the bottom, so it never starts.

 

 

EDIT: Maybe make a very small example, with 2 choices (1 is also possible, but a bit silly), and you can only buy 1 thing or so (or just print("buy thing") instead of the administration of actually buying anything). Having a small example is easier to understand and to discuss if you don't find the answer.

pretty stoked that I got a 106 IQ (proficient - middle range) score in python on pluralsight, considering I'm just starting and think of myself as a beginner.

The ranges are novice, proficient, expert.

I expected to get a novice but compared to my peers I'm middle range. Which motivates me even more to keep going with python, I've also decided to get started with unity and eventually learn c++ for unreal engine.

I know these goals are quite mountainous considering how much trouble I seem to be having so early on in the game.

But I have overcome greater obstacles in the past, I think my goals are realistic and I'm optimistic. I also don't consider myself a coder yet, only a designer. If I can get to expert level IQ and finish a game (even a reverse engineered platform or arcade game) then I will be able to say I'm a coder, not simply that I'm learning to code.

Thanks Alberth for your feedback that is all very helpful. I have a better understanding of what I'm doing now.

Okay I'll have a go at reusing some of my code to build a smaller program. And then refine it so it works and works well.

 

btw I use Idle because it came with python.

http://polydina.com

Advertisement
On 9/26/2017 at 12:48 AM, WinterDragon said:

btw I use Idle because it came with python.

No problem with this. It just doesn't have any tools build in. In fact for beginning this is better because it forces you to double check your code.

Good to here your still at it. Text adventure games are the basics of all games, so learning this will help you understand how all games work.

okay so I've taken a break from my adventure game to study up on my fundamentals - been working my way through my first python book goal = 60pgs/wk by crunching down and working through 60pgs in one day, 1day/wk and any exercises and fixing bugs + asking questions, etc.

I got an assignment to create a fortune cookie simulator but I've only got a little bit of info about def methods. so I keep screwing them up. also I had to skip forward in the book (specifically from page 85 to page 356) to find out how to add a end_game feature which requires a  def method. Problem is the idle processor says there are no bugs but when I run it nothing happens. first thought of course was I haven't told it to do anything because one of my lines made it skip the rest. but I don't think that's what's happening, because I have 5 loops and rand.range calls 5 numbers from 0-4. so that checks out I think. any clues as to why it might not be doing anything? my instinct says the problem is in the end_game method itself.

 

import random

def end_game(self):
    end_message = games.Message(value = "game over",
                                size = 90,
                                color = color.red,
                                x = games.screen.width/2,
                                y = games.screen.height/2,
                                lifetime = 5 * games.screen.fps,
                                after_death = games.screen.quit)
    games.screen.add(end_message)
    
def game():
    nmCookie = random.randrange(5)
    begin = input ("cookie time, open your fortune cookie")
    if nmCookie < 1:
        print ("you are going to die someday")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or "y":
            end_game()
        game()
        
    elif nmCookie == 1:
        print ("you just ate a cookie")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or "y":
            end_game()
        game()

    elif nmCookie == 2:
        print ("you are going to eat another cookie")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or "y":
            end_game()
        game()
        
    elif nmCookie == 3:
        print ("you like cookies")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or "y":
            end_game()
        game()
        
    elif nmCookie == 4:
        print ("you will have a gargantuan legacy")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or "y":
            end_game()
        game()
    else: end_game()
    

#added call game

game ()

edit: solved it - forgot to call "game"

 

#now it's telling me random not defined, but the book didn't tell me I needed to define random, am I missing something?

http://polydina.com

I got the game to work after a few edits:

but I still can't break out of the game.

break initiates the error message "break outside loop"


import random
	
def end_game():
    end_message = ("game over")
    print (end_message)
    
    
    
def game():
    nmCookie = random.randrange(5)
    begin = input ("cookie time, open your fortune cookie")
    if nmCookie < 1:
        print ("you are going to die someday")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or againPLay != "y":
            end_game()
        game()
        
    elif nmCookie == 1:
        print ("you just ate a cookie")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or againPLay != "y":
            end_game()
        game()
	    elif nmCookie == 2:
        print ("you are going to eat another cookie")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or againPLay != "y":
            end_game()
        game()
        
    elif nmCookie == 3:
        print ("you like cookies")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or againPLay != "y":
            end_game()
        game()
        
    elif nmCookie == 4:
        print ("you will have a gargantuan legacy")
        againPlay = input ("Still hungry")
        if againPlay != "Y" or againPLay != "y":
            end_game()
        game()
    else: end_game()
    
            
game()
	


 

http://polydina.com

What break are you talking about?

You can't break out of functions, you can only break out of loops. If you want to 'break' out of a function, you can use a return statement. LIke this:


if againPlay != "Y" or againPLay != "y":
	end_game()
	return
game()

The return statement stops the execution of the function and returns a value. But in this case, we don't want to return any value, just stop the execution.

This will achieve the same result as writing:


if againPlay != "Y" or againPLay != "y":
	end_game()
else:
	game()

The problem with your code is that 'end_game' (whatever the name might say) isn't really ending the game or 'breaking' out of the code. You are still calling the game() function again. If you get confused then it's a good practice usually to use names that better describe the function. LIke, in this case, 'printEndMessage' or 'endGameMessage', for example.

Advertisement

It's normal to use a while loop for this:


#So while the GameRun is true it keeps playing the game but when it's false the game stops
def while(GameRun == True):
  GameHere();
  GameRun = False;

 

Attached is a game I made as practice. I stopped when I got to the boring part and never finished it.

AdventureGame.py

The game loop is at the bottom and looks like this:


#This is the game loop
def GameLoop(InPlayer):
    while(InPlayer.GameRun == True):
        InPlayer.PlayerPromt()
        print("#"*5)

It takes a prompt from the player and if it's "Q" the game quits.

Take a look at the python file, it shows a basic structure for a game.

 

Edit: Double clicking the file should run it.

yea thanks that's all really helpful. I haven't actually got upto GUIs yet. so I guess there will be more about that when I do.

 

I just ran the game, I'll have a look at the code now and it will give me more insight.

http://polydina.com

This topic is closed to new replies.

Advertisement