0

Help. Has Python changed in version 3.6.5, or is there something wrong with my code?!

The following doesn't work:

import os

def main():
console = input("...")
rom = input("...")

The following does work:

import os

console = input("...")
rom = input("...")

def main() :

Comments
  • 0
    You dont call main() in your first script?
    And how the second works if you dont put anything in the function (like pass) should be EOF.

    Why import os (not needed for that snippet)?
  • 0
    @hube Well that's my problem, in college they taught me to use, "def main() :" But for some reason that isn't working for me unless I add a print statement. I even have a print statement lower down in the code that uses both variables and a string.

    Example:

    def main() :
    x = input("...")
    print(x)

    In my code:

    def main():
    x = input("...")
    y = input("...")
    print("..."+ x + y)
  • 1
    I am not sure to understand your issue, input() is called when you instantiate it in your variable.
    You just need to call main()
  • 0
    def main():
    x = input("What is your name?")
    y = int(input("And your age?"))
    print("My name is "+ x + " and my age is " + str(y))
    main()

    I am not sure to understand at all your issue, input is for getting an user interaction here a string or an int with a cast then print output it, (ofc you can return it in your function then print the function)

    print("%s, %d" % (x,y))) is better in general
  • 0
    @hube I figured out my problem. I forgot to close the main function with "main()" at the very bottom of the file. Thanks your last comment reminded me about it.
  • 2
    @JohnScott that is not a "closing", that is a call.
    An commented good practice example:

    def main(): # function definition
    x = input("How old are you? ") # read the input until a newline and put it into x
    print("You are %s years old" %(x)) #replace %s with the the variable x and write it into the standard output

    if __name__ == "__main__": # if this file is run directly and not imported, execute the following
    main() #execute the function main without arguments
  • 0
    @stop Thanks for clarifying it, but I was trying to be metaphorical. Python doesn't have parentheses but you still have to end the file with "main()" and write the rest of the code in between. Until today I always thought this resembled parentheses or a semicolon. Guess I was wrong.
  • 1
    @JohnScott defining a function main and calling it without putting it into the 'if __name__ == "__main__"' block is bad style, because everything runs even if the file is being imported, with the block it can run if the file is called and is at the same time usable as module.
  • 1
    as a complete beginner to this topic and after reading the helpful comments i strongly recommend looking at the documentation chapters about functions and how to use them
    you do not end functions with something, that's done with intendation. to make use of a function you have to call it wherever you need it. if you have your whole code in just one function calling it in the end, you might not use a function at all just running the script from top to bottom...
  • 0
    @erroronline1 That is a valid point. And I'm not a complete novice to python, I took a college course for it. My issue is that without calling the function at the end certain functions (I think that's the right terminology) wouldn't work.

    I found out that (in this situation) you can make an "input" statement (which didn't showup in the shell) run if you call them individually in a "print" statement. Albeit it's the worst possible way of doing it.
  • 2
    none of the code within the function does run if you don't call the function.

    if you take a glass for the function and the juice for your code, you can pour in whatever you want but you can't drink it unless you pick up the glass. now you can use the glass before eating, between bites or at the end of your breakfast.
    if you just care for the juice without breakfast context you could just drink from the bottle.

    sorry for sounding excited, but it kind of grinds my gears reading you had a course and still having an issue handling, using or understanding functions. i do not want to ashame you but that raises my question what they even taught you. i know not everyone likes sololearn and i do not want to overadvertise it but maybe taking its python tutorial may clear things up a bit.
  • 0
    @erroronline1 it's been a while okay. I'm on holiday. I just forgot about it.
Add Comment