Re: Need help with a script

@3

microsoftsam203 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 runs
I 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 paul
n 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 1

def 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 instead
now
# version 2

def 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 -1
In this way you don't have to worry about RecursionError.
If you haven't understood something please feel free to ask.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : redfox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : redfox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Munawar via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : microsoftsam203 via Audiogames-reflector

Reply via email to