Identify the day of the week of a certain date

0
One of the questions of a college assignment is giving work for the whole class, so far no one has been able to answer question 4. A friend of mine tried with this code, but it does not work right, because depending on the entries, the month leaves 13 , 14, 19 and countless other non-existent months. Could someone give me a hand? PS: It needs to be in python 3.

  

Question : You will have a wonderful vacation starting on the 3rd, Wednesday. You will return from your vacation after 137 nights (wow!). Write a program that asks for the day of the month and the day of the week you are going to travel and asks for the number of days you will be on vacation and prints the day of the week you will return.

Code:

def questao4(m,s,f):
    f = f+m
    t = f%30
    rs = t%7
    return print("Mês =", t,"Dia da semana =", rs)
questao4(9,3,1)
    
asked by anonymous 27.04.2018 / 02:41

2 answers

3

This issue can be easily resolved with the datetime library, creating an object date which represents the date and a timedelta that represents the time interval to be considered.

from datetime import date, timedelta

DIAS = [
    'Segunda-feira',
    'Terça-feira',
    'Quarta-feira',
    'Quinta-Feira',
    'Sexta-feira',
    'Sábado',
    'Domingo'
]

inicio = date(year=2018, month=4, day=26)
ferias = timedelta(days=137)
final = inicio + ferias

print(DIAS[final.weekday()])  # Segunda-feira

See working at Repl.it | Ideone | GitHub GIST

But as the focus of exercise is probably math, you just have to think about the operations to be done. If I go on vacation on day 3, Wednesday, and stay in it for 137 nights, that means I'll stay 19 full weeks plus 4 days. At the end of the 19 weeks, I will know that it will be a Wednesday again, so I only have to check which day of the week it will be 4 days later (but as it is a college exercise, I leave the implementation of this logic with you).

    
27.04.2018 / 03:02
0
x = input("Quantos dias voce ficara de ferias? ")
duracao_ferias = int(x)


y = input("Digite o dia do mes que voce ira viajar: ")
dia_viagem = int(y)

dia_retorno = duracao_ferias + dia_viagem
if (dia_retorno > 30):
    dia_retorno = (duracao_ferias + dia_viagem) % 30

z = input("Digite o dia da semana que voce ira viajar: ")

if (z == "Domingo"): 
    z = 0
if (z == "Segunda-feira"): 
    z = 1
if (z == "Terca-feira"): 
    z = 2    
if (z == "Quarta-feira"): 
    z = 3    
if (z == "Quinta-feira"): 
    z = 4    
if (z == "Sexta-feira"): 
    z = 5    
if (z == "Sabado"): 
    z = 6

dia_semana_retorno = duracao_ferias % 7 + z
if (dia_semana_retorno > 6):
    dia_semana_retorno = (duracao_ferias % 7 + z) - 7

if (dia_semana_retorno == 0):
    dia_semana_retorno = "Domingo"
if (dia_semana_retorno == 1):
    dia_semana_retorno = "Segunda-feira"
if (dia_semana_retorno == 2):
    dia_semana_retorno = "Terca-feira"
if (dia_semana_retorno == 3):
    dia_semana_retorno = "Quarta-feira"
if (dia_semana_retorno == 4):
    dia_semana_retorno = "Quinta-feira"
if (dia_semana_retorno == 5):
    dia_semana_retorno = "Sexta-feira"
if (dia_semana_retorno == 6):
    dia_semana_retorno = "Sabado"

print("Voce voltara no dia",dia_retorno,dia_semana_retorno)
    
02.10.2018 / 18:25