我已经在代码的前一部分中定义了日期和月份。我需要这个程序运行,当一天是少于7。我要求一个投入的开支在过去7天。
问题:如果输入的日期是4,例如,4-4(天)是第0天(不存在),4-5是天-1,4-6是天-2,4-7是天-3。我需要的是第4-4天= 31或30或28或29 (已经定义),4-5= 30,4-6= 29。
我知道这是糟糕的结构,我很抱歉,英语不是我的第一语言。如果不是这样理解的话,我会尽力弄清楚的。
listOfSpendings = []
x = 0
while x < 7:
if day - x <=0:
month = month - 1
dayDiff= ###SOMETHING I DUNNOOOO
day = monthlenght - dayDiff
print ("How many liters did you spend on the day", day - x, "of", month)
spendings = input()
while True:
try:
spendings = int(spendings)
if spendings < 0:
spendings = input ("Insert a value =! 0")
else:
break
except ValueError:
spendings = input("Incorrect value, correct")
x = x+1
listOfSpendings.append(spendings)
sumSpendings = sum (listOfSpendings)
发布于 2018-11-15 21:40:28
您的代码在几个月内也会遇到负数。使用建议的日期时间库,您可以执行以下操作:
from datetime import datetime, timedelta
list_of_spendings = []
# Month number
for day in [(datetime.now()-timedelta(x)) for x in range(7)]:
print('How many liters did you spend on the day {} of {}'.format(day.day, day.month))
#Rest of your code
或
# Month name
for day in [(datetime.now()-timedelta(x)) for x in range(7)]:
print('How many liters did you spend on the day {} of {}'.format(day.day, day.strftime('%B')))
#Rest of your code
或
# Short month name
for day in [(datetime.now()-timedelta(x)) for x in range(7)]:
print('How many liters did you spend on the day {} of {}'.format(day.day, day.strftime('%b')))
#Rest of your code
发布于 2018-11-15 22:19:58
我就是这么想出来的。"Dia“和"mes”是先前定义的。
翻译:
listaGastos = listOfSpendings
gastos = spendings
dia = day
mes = month
DuracMes = monthlenght
diaC = currentDay
mesC = currentMonth
代码:
DuracMes = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
listaGastos = []
x = 0
diaC = dia
mesC = mes
while x < 7:
dia = diaC - x
if dia <= 0:
mes = mesC - 1
diaDif = diaC - 1
dia = DuracMes [mes - 1] + dia
print("Quantos litros de soro fisiologico foram gastos no dia", dia, "de", mes)
gastos = input()
while True:
try:
gastos = int (gastos)
if gastos < 0:
gastos = input ("Introduza um valor diferente de 0: ")
else:
break
except ValueError:
gastos = input ("Nao inseriu um valor adequado, quantos litros de soro fisiologico foram gastos nos ultimos sete dias? ")
x += 1
listaGastos.append(gastos)
sumGastos = sum (listaGastos)
https://stackoverflow.com/questions/53327743
复制相似问题