Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python count program wont work as expected

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading