1

I have this simple code

count_down = int(input("How many left left until the weekend?: "))
while count_down:
    print(count_down)
    count_down -= 1
print("It's the weekend!")

The output is a countdown: example:

5
4
3
2
1
It's the weekend!

I'm pretty new to python. Is there a way that I can make it so that the user can input a day of the week and make it start from that day. An example would be if the persons puts down tuesday it would count down

Tuesday
Wednesday
Thursday
Friday
It's the weekend!
Good_soup
  • 19
  • 3

3 Answers3

0

If you'd like for the user to input a day of the week and then countdown from that day until you reach the weekend, you can do so with the following code:

weekends = ['Saturday','Sunday']
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday','It\'s the weekend!']

currentDay = input('What day is it?')

if currentDay in weekdays:
    print(currentDay)
    newLst = weekdays[weekdays.index(currentDay)+1:]
    while len(newLst) > 0:
        print(newLst[0])
        newLst = newLst[1:]
elif currentDay in weekends:
    print("It\'s already the weekend!")

The basis of this code is essentially slicing lists.

Machetes0602
  • 366
  • 2
  • 8
0

If you don’t want to import calendar you can make a simple list of the days of the week:

count_down = input("What day should we start at?: ")
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

for i in range(len(week)): #this will loop though the list where i is the index
   if week[i] == count_down: #then we check each index to see if it is the correct string
      count = i 
      while count < len(week): #then we count from that index
         print(week[count])
print("it is the weekend!")

If you are new to python and you are not interested in improving time complexity this will work.

dent
  • 1
  • 1
0

One of the approaches can be:

week_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'It\'s a weekend']
curr_day = input("Current Day: ")
if (curr_day.lower() in ['saturday', 'sunday']):
    print ('You are in weekend')
else:
    start = None
    for day in week_days:
        if (curr_day.lower() == day.lower()):
            print (curr_day)
            start = True
        if curr_day.lower() != day.lower() and start:
            print (day)
            
    if (not start):
        print ('Invalid Day Given')

Output:

Current Day: Tuesday
Tuesday
Wednesday
Thursday
Friday
It's a weekend
Bhagyesh Dudhediya
  • 1,800
  • 1
  • 13
  • 16