Hack Forums
[Python] Start is not defined - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Start is not defined (/showthread.php?tid=5711857)



Start is not defined - Deevez - Sep 6, 2017

After noon everybody. So, I'm piecing together just a basic project for school to help teach the students who are just starting Python. Now, I'm running into an issue that is really confusing me. Maybe you guys can help me out.

Code
import sys
import random
import time

print ("Welcome to D's Python Game!")
print ()
print ("1.) Play Game")
print ("2.) Load Game")
print ("3.) Quit Game")
print ()
option = input("-> ")
if option == "1":
    start()
elif option == "2":
    pass
elif option == "3":
    sys.exit()
else:
    main()

def start():
    print()
    print ("You wake up in a dark, cold, wet dungeon.")
    print ("The cell is small. It's filled with a couple inches of musty water.")
    print ("There is a skeleton in the corner of the cell.")
    print ("It appears to be holding something.")
    print()
    print ("What do you do?")
    print()
    print ("1.) Take the item")
    print ("2.) OH HAIL NAW!")
    print()
    option = input("-> ")
    if option == "1":
        start1()
    elif option == "2":
        sys.exit()
    else:
        start()

So basically the issue, is that upon running the module, I get through the first stage which is the welcome menu. But once you press "1", it's supposed to go to "start", however it comes back with "start is not defined". I've tried removing the def and un-indenting everything. Someone suggested making a variable called start. I've tried a range of different solutions and it always goes back to "Start is not defined".


* - Charger - Sep 6, 2017

I'm pretty sure start needs to be defined before you call it unless you do the main contructor at the end.


* - Deevez - Sep 6, 2017

(Sep 6, 2017 09:43 AM)Charger Wrote:  I'm pretty sure start needs to be defined before you call it unless you do the main contructor at the end.

How would you define start before it is called?


* - Nachfolger - Sep 6, 2017

Have you tried changing the function name?

I don't know python, but it could be a protected word.


* - Charger - Sep 6, 2017

(Sep 6, 2017 09:45 AM)Deevez Wrote:  How would you define start before it is called?

Logically you don't
Have all of your functions in what ever order but at the end of your file is where to invoke the script.
https://stackoverflow.com/questions/4041238/why-use-def-main


* - Deevez - Sep 6, 2017

(Sep 6, 2017 09:47 AM)Charger Wrote:  Logically you don't
Have all of your functions in what ever order but at the end of your file is where to invoke the script.
https://stackoverflow.com/questions/4041238/why-use-def-main

This is extremely helpful, I'll get to work and see where I end up. Thank you!


(Sep 6, 2017 09:47 AM)Charger Wrote:  Logically you don't
Have all of your functions in what ever order but at the end of your file is where to invoke the script.
https://stackoverflow.com/questions/4041238/why-use-def-main

So here's something. A classmate of mine who is helping me out showed me his code. And his works "flawlessly" (as far as his functions go, user input for a name not so much).

Code
# Name: ScarletJett Development
# StartDate: 8/31/2017

import random
import time
import sys
import os

class Player:
    def _init_(self,name):
        self.name = name;
        self.maxHealth = 100;
        self.Health = maxHealth;
        self.attack = 10;

def main():
    print ("Welcome to my game!\n")
    print ()
    print ("1.) Start")
    print ("2.) Load")
    print ("3.) Exit")
    print()
    option = input("-> ")
    if option == "1":
        start()
    elif option == "2":
        pass
    elif option == "3":
        sys.exit()
    else:
        main()

def start():
    print ("Hello! What is your name?")
    print()
    option = input("-> ")
    global PlayerIG
    PlayerIG = Player()
    start1()

def start1():
    print ("Hello %s, how are you?") % PlayerIG.name

main()

And looking at it, his is set up exactly like mine roughly. Why is it that his works and mine doesn't?


* - TheMarkMan - Sep 6, 2017

The issue is exacty what charger said. Look at your friends code. He defines a function called main before he defines start, but he does not call main/run any code until after all the functions have been defined


* - Charger - Sep 6, 2017

(Sep 6, 2017 10:05 AM)TheMarkMan Wrote:  The issue is exacty what charger said. Look at your friends code. He declares a function called main before he declares start, but he does not call main/run any code until after all the functions have been defined

This.

Python is executed top to bottom like many languages. You can't call anything before it exists. The code I provided simply tells Python to execute this function when the script has been loaded.


* - Deevez - Sep 6, 2017

(Sep 6, 2017 10:05 AM)TheMarkMan Wrote:  The issue is exacty what charger said. Look at your friends code. He defines a function called main before he defines start, but he does not call main/run any code until after all the functions have been defined

Fixed. Stupid and simple. Thank you, it works fine now!


* - koolkid - Sep 6, 2017

(Sep 6, 2017 09:40 AM)Deevez Wrote:  After noon everybody. So, I'm piecing together just a basic project for school to help teach the students who are just starting Python. Now, I'm running into an issue that is really confusing me. Maybe you guys can help me out.

Code
import sys
import random
import time

print ("Welcome to D's Python Game!")
print ()
print ("1.) Play Game")
print ("2.) Load Game")
print ("3.) Quit Game")
print ()
option = input("-> ")
if option == "1":
    start()
elif option == "2":
    pass
elif option == "3":
    sys.exit()
else:
    main()

def start():
    print()
    print ("You wake up in a dark, cold, wet dungeon.")
    print ("The cell is small. It's filled with a couple inches of musty water.")
    print ("There is a skeleton in the corner of the cell.")
    print ("It appears to be holding something.")
    print()
    print ("What do you do?")
    print()
    print ("1.) Take the item")
    print ("2.) OH HAIL NAW!")
    print()
    option = input("-> ")
    if option == "1":
        start1()
    elif option == "2":
        sys.exit()
    else:
        start()

So basically the issue, is that upon running the module, I get through the first stage which is the welcome menu. But once you press "1", it's supposed to go to "start", however it comes back with "start is not defined". I've tried removing the def and un-indenting everything. Someone suggested making a variable called start. I've tried a range of different solutions and it always goes back to "Start is not defined".

nice i see the error put the start function at the start of your code.

are you planning on building a dungeon game ??