Re: Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Re: Need help with a script Hello,Thanks all for the help.I have looked at the code now and I still have a question. countdown(n-1)Why is this line indented like that? I mean why has this line 8 spaces instead of 4? It's not a part of the els function.@3 I meant if this developers section of the audiogames forum was for development in general or only for audiogame development. URL: https://forum.audiogames.net/post/472247/#p472247 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Re: Need help with a script Hello,Thanks all for the help.@3 I meant if this developers section of the audiogames forum was for development in general or only for audiogame development. URL: https://forum.audiogames.net/post/472247/#p472247 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script The good thing about languages like Python is that your computer won't crash because you blew your stack. It's considered a safe environment to play around in since you can overflow the stack without much in the way of consequences. So don't worry about crashing your system.The def keyword defines a function. And functions return values (or nothing at all.) Functions can call themselves if they want to. All these functions get put on something called a stack. So if a function calls itself over and over, eventually you reach the limit of your stack size and Python will give an error. URL: https://forum.audiogames.net/post/472169/#p472169 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script @7, yep. If you have a while loop in a recursive function it will run infinitely and would most likely increase the possibility of a stack overflow or something worse. I.e.: if you have a function and a while loop that repeatedly calls that function, then you have a function with a loop that calls itself. That called function then loops and calls itself, and so on and so on until your program goos *** B O O M ***. URL: https://forum.audiogames.net/post/472165/#p472165 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script @7, yep. If you have a while loop in a recursive function it will run infinitely and would most likely increase the possibility of a stack overflow or something worse. URL: https://forum.audiogames.net/post/472165/#p472165 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script @6, are you refering to my suggestion of using a while loop?I'm not very experienced in python and didn't realize that you could return from a def like that. URL: https://forum.audiogames.net/post/472147/#p472147 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script @4, no, no, never do this. I'll explain why in a moment.@OP, what you've made is called a recursive function. That is, you've made a function that "recursively" (or repeatedly) calls itself with either the same or modified arguments. This is a bit of an advanced form of programming (since recursion can be very dangerous if misused). Amerikranian modified your input to:1) prevent the function from endlessly calling itself, which would eventually cause the Python interpreter to crash; and2) not make the system ask for input when the function is called because that breaks the entire idea of recursion.What you have written here is, in short, an algorithm. However, your original version, while being an algorithm, is a horrible method of algorithmic programming because the algorithm has to wait on the user every time it wants to do everything. The idea of an algorithm is to get all the data from the user or from whatever source of data it needs to use as "input" of sorts, then perform a series of "steps" to achieve an eventual outcome. Amerikranian's version of your algorithm breaks down to this:Ask the user for input to get the number to start the countdown from. Convert that to an integer.When the algorithm starts:Check the number and determine if it is less than zero. If it is, abort.If the above condition is false, repeat the algorithm, subtracting one from the given number.If the algorithm was not written this way, it would keep subtracting one from the number; however, since Python's integer type's range is negative infinity to infinity, the algorithm would run for ever, and not only blast your screen with numbers and information, but would also either (1) crash the interpreter or (2) crash your computers operating system because it ran out of memory. URL: https://forum.audiogames.net/post/472088/#p472088 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script @3microsoftsam203 wrote:#Don't get the user input in the function, as doing so will basically cause them to reenter a number every time a program runsI don't get this. Can you explain why this happens?You are adopting a technique called recursion. Recursion is when you call the function from within it's self. Since the first statement that you have in the function is asking for input, the function will keep asking for input when it gets called from it's self. That's why you should keep the input statement out of it.microsoftsam203 wrote:Why does input only work for strings and not for integers? I don't see why input cant work for ints.Input returns a string which is the content of what you've typed. If you do something like:n = input("enter your name") # name entered is pauln will have the value paul. You can not convert the string paul to an integer.Instead if you insert a numerical value the int() function will work properly.microsoftsam203 wrote:Why does it break after it gets to 0? I mean why doesn't it keep printing blastoff!?the function doesn't really break. It should print blast off. The problem is since you do not specify when the function should terminate, the function will keep executing until it raises a RecursionError.Now, I have 2 versions as well, which should fix your problem# version 1def count_down(n): if n <= 0: print("blast off") return # we exit from the function so that the function doesn't keep doing recursion else: print(n) count_down(n-1)n = input("enter a number")if n.isdigit(): n = int(n)else: print("invalid value")count_down(n)str.isdigit() returns true if a value is a digit (number) or false if not.By doing this you can make sure that the program will never crash but it will return a friendly error insteadnow# version 2def count_down(n): for i in range(n, -1, -1): if i <= 0: print("blast of") else: print(i)n = input("enter a number")if n.isdigit(): n = int(n)else: print("invalid value")count_down(n)Probaly this second version is easier to read but let me explain it anyway.The function instead of doing recursion will do an iteration from n to -1In this way you don't have to worry about RecursionError.If you haven't understood something please feel free to ask. URL: https://forum.audiogames.net/post/472072/#p472072 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script microsoftsam203 wrote:Hello,Thanks a lot for the help.I still have some questions. #Don't get the user input in the function, as doing so will basically cause them to reenter a number every time a program runsI don't get this. Can you explain why this happens?The way you coded it... Everytime the code loops, it calls the countdown function. The issue with this is that at the start of the countdown function, it will ask for input, so it will start counting, and imidiately stop because your asking for a new number.Why does input only work for strings and not for integers? I don't see why input cant work for ints.The input function just basically makes an input box. As shown @2, it is very easy to convert it to an integer.Why does it break after it gets to 0? I mean why doesn't it keep printing blastoff!?I'm not a hundred percent on the syntax for python, but the reason it keeps going is that you call countdown again.What I would do is put that in a while loop and break after you print blastoff.Is this forum only for game development or for development in general?Please elaborate on this question.Thanks a lot. URL: https://forum.audiogames.net/post/472030/#p472030 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Re: Need help with a script Hello,Thanks a lot for the help.I still have some questions. #Don't get the user input in the function, as doing so will basically cause them to reenter a number every time a program runsI don't get this. Can you explain why this happens?Why does input only work for strings and not for integers? I don't see why input cant work for ints.Why does it break after it gets to 0? I mean why doesn't it keep printing blastoff!?Is this forum only for game development or for development in general?Thanks a lot. URL: https://forum.audiogames.net/post/472004/#p472004 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Re: Need help with a script Hello,Thanks a lot for the help.I still have some questions. #Don't get the user input in the function, as doing so will basically cause them to reenter a number every time a program runsI don't get this. Can you explain why this happens?Why does input only work for strings and not for integers? I don't see why input cant work for ints.Is this forum only for game development or for development in general?Thanks a lot. URL: https://forum.audiogames.net/post/472004/#p472004 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Need help with a script
Re: Need help with a script I will provide the fixed script and will comment the things I've changed and or addeddef countdown(n): #Don't get the user input in the function, as doing so will basically cause them to reenter a number every time a program runs if n <= 0: print('blast off!') else: print(n) countdown(n-1) #now, here's the tricky part. input() takes the user input in as a string. This means that we will have to convert whatever you've entered into an integer before passing it along to the countdown function #I'll write two versions: Here is the first one, which is a bit easier to understand n = input('Put a number in.') #Now, convert the thing to integer by using int() n = int(n) countdown(n) #Here's a second version, basically the same thing but condensed countdown(int(input('Put a number in.')))End codeI should mention that this will crash should you enter an invalid string to be converted to an integer. There is a way to stop that as well, but I digress.Hope this was helpful. URL: https://forum.audiogames.net/post/471947/#p471947 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Need help with a script Hello all,I'm learning python with the help of a book called think python 2. In chapter 6 there is an exercyse where you have to make a program that counts down from 10 to 0. When it reaches 0 it should print blast off.Now as an extra exercyse, I thought I'm going to let the user put the number in themselves instead of having the number written in the script already, but there is something wrong in my code.I have tried everything I could think of and I wanted to ask if somebody could have a look into it and tell me what is wrong.Codedef countdown(n): n = input('Put a number in.') if n <= 0: print('blastoff') else: print(n) countdown(n-1)countdown(n)Thanks a lot and have a good day. URL: https://forum.audiogames.net/post/471895/#p471895 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Need help with a script
2019-11-02
Thread
AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
Need help with a script Hello all,I'm learning python with the help of the book think python 2.In chapter 6 there is an exercyse where you have to make a program that counts down from 10 to 0 and when it reaches 0 it prints blast off.Now as an extra exercyse, I thought I'm going to let the user put the number in themselves instead of having the number written in the script already, but there is something wrong in my code.I have tried everything I could think of and I wanted to ask if somebody could have a look into it and tell me what is wrong.Codedef countdown(n): n = input('Put a number in.') if n <= 0: print('blastoff') else: print(n) countdown(n-1)countdown(n)Thanks a lot and have a good day. URL: https://forum.audiogames.net/post/471895/#p471895 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector