Python count program wont work as expected

Question:

im new to python and i have a count program that i dont know whats wrong with, for some reason, when trying to run it, when i put in Y or N for slow count, it just does nothing and exits with code 0.

Heres my code:


def cnt(start, end):
    for x in range(start, end+1):
        print(x)
        time.sleep(1)
    print("Done!")

def count():
    num = int(input("Enter a number to count to: "))
    #Slow count meaning counting 1 by 1 in console.
    slcnt = bool(input("Would you like to slow count? Y/N: "))
    if slcnt == "Y":
        cnt(0, num)
    elif slcnt == "N":
        for i in range(num):
            print(i)
        print("Done")
            
count()
Asked By: Ralph Turner

||

Answers:

The problem is this line slcnt = bool(input("Would you like to slow count? Y/N: ")), you can’t make it boolean because you are asking for a character. It may be fixed like this:

import time

def cnt(start, end):
    for x in range(start, end+1):
        print(x)
        time.sleep(1)
    print("Done!")

def count():
    num = int(input("Enter a number to count to: "))
    #Slow count meaning counting 1 by 1 in console.
    slcnt = input("Would you like to slow count? Y/N: ").upper()
    if slcnt == "Y":
        cnt(0, num)
    elif slcnt == "N":
        for i in range(num):
            print(i)
        print("Done")

count()

You didn’t add the line import time, but I guess it was a mistake when you pasted the code.
I also added upper() to the input function, so the script will accept Y/N and y/n.

Answered By: Joan Lara Ganau
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.