0

Loop that ask for names, numbers, and numbers to skip. Enter your name: Alex Input your starting number: 5 What number do you want to skip: 3 Do you have more numbers to skip (Y/N): Yes Please only input Y or N. Do you have more number to skip (Y/N): Y What number do you want to skip: 1 Do you have more numbers to skip (Y/N): N Alex is starting to count from 5. Start! 5... 4... 2... 0!

My Code are below but I think anyone may change it because my code looks not so good:

text = input("Enter your name: ")
print(text)
text1 = input("Input your starting number:")
number1 = int(text1)
text2 = input("What number do you want to skip:")
number2 = int(text2)

def count_down_skip(start, skip=0):

    
    return [num for num in reversed(range(start + 1)) if num != skip]


print("... ".join(map(str, count_down_skip(5, 3))) + "!")

def yes_or_no(question):
    reply = str(input(question+' (y/n): ')).lower().strip()
    if reply[0] == 'y':
        return 1
    elif reply[0] == 'n':
        return 0
    else:
        return yes_or_no("Please only input Y or N. ")


while True:
    # DRAW PLOT HERE;
    if(yes_or_no('Do you have more numbers to skip')):
      
        break
text3 = input("What number do you want to skip:")
number3 = int(text2)

def count_down_skip(start, skip=0):
    """
    Counting down a sequence with a skip value,
    from a defined start point in reversed order.

    Args:
        start: start loop index.
        skip: number to skip over.

    Returns:
        (list): skipped list.

    """
    return [num for num in reversed(range(start + 1)) if num != skip]


print("... ".join(map(str, count_down_skip(5, 1))) + "!")

def yes_or_no(question):
    reply = str(input(question+' (y/n): ')).lower().strip()
    if reply[0] == 'y':
        return 0
    elif reply[0] == 'n':
        return 1
    else:
        return yes_or_no("Please only input Y or N. ")


while True:
    # DRAW PLOT HERE;
    if(yes_or_no('Do you have more numbers to skip')):
        break
print("Alex is starting to count from 5") 
print("start")

#print("... ".join(map(str, count_down_skip(5, 1))) + "!")

def count_down_skip(start, skip = []):
    return [num for num in reversed(range(start + 1)) if num not in skip]

print("... ".join(map(str,count_down_skip(5,[1,3]))) + "!")

def count_down_skip(start, skip=0):

    return [num for num in reversed(range(start + 1)) if num not in skip]


print("... ".join(map(str, count_down_skip(10, [1,4,3]))) + "!")

And I think if I want to skip some numbers like from 10 to 0 without 1 4 3, I should use something like number() or int(), right? anyone can assist to change my code? It can work but a little bit imperfect.

1 Answer 1

1

Nice try, this is how I would do it, see below:

def main():
    skipable = set()

    name = input("What is your name?")
    max_number = input("What number do you want to count down from?")
    if type(int(max_number)) != int:
        raise Exception('You done messed up, please enter an integer next time')
    max_number = int(max_number)
    skipping = True
    while skipping:
        skip = input("Would you like to skip any numbers in the count down? Y/N")
        if skip == 'Y':
            number_to_skip = input("What number would you like to skip")
            try:
                number_to_skip = int(number_to_skip)
                skipable.add(number_to_skip)
            except Exception as e:
                print('Try entering an integer next time')
        elif skip == 'N':
            skipping = False
        else:
            print('Please Enter Y or N')

    numbers_to_print = [i for i in range(0, max_number+1) if i not in skipable]
    print(f'{name} is counting:')
    numbers_to_print.sort(reverse=True)
    print('...'.join(map(str, numbers_to_print)) + '!')


if __name__ == '__main__':
    main()

results look like this:

What is your name?>? James
What number do you want to count down from?>? 5
Would you like to skip any numbers in the count down? Y/N>? Y
What number would you like to skip>? 2
Would you like to skip any numbers in the count down? Y/N>? N
James is counting:
5...4...3...1...0!

EDIT Oh and if you want it to count down, you could down just throw a:

numbers_to_print.sort(reverse=True)

in there and it will sort the list descending.

Edit 2. Changed the code above to match question better

Sign up to request clarification or add additional context in comments.

5 Comments

You are so excellent! it works out well. And I added numbers_to_print.sort(reverse=True). But if I hope the out put format is 5...4...2...0! the print should be print("... ".join(map(str,i)))+ "!") right?!
I try to use print("... ".join(map(str,main(i)))+ "!") but the error happened....
the map function takes a function and an itterable, let me change the code above
Have changed the code above, try that. Does that make sense why that works now?
You are the best!!!!! it works out! Thank you soooooooo much! May all lucky things around you forever and ever and ever!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.