def today(): day = '星期六' if day == '星期天' or ...: print('今天周日') else: print('今天周六') today()
时间: 2024-05-17 10:18:10 浏览: 33
这是一个 Python 代码段,函数名为 `today()`。该函数首先定义了一个变量 `day`,并将其赋值为字符串 `'星期六'`。接下来,该函数使用条件语句来检查今天是星期天还是星期六。如果今天是星期天或其他未给出的情况,那么将会输出字符串 `'今天周日'`。否则,也就是今天是星期六的情况下,将会输出字符串 `'今天周六'`。最后,该函数被调用,将输出 `'今天周六'`。
相关问题
修正以下python代码:class Ticket: def __init__(self, day, discount=False): self.day = day self.discount = discount def get_ticket_price(self, num): if self.day == "周一": print("博物馆周一闭馆,不提供售票服务") elif self.day == "周二" or "周三" or "周四" or "周五": if self.discount: price = 80 else: price = 100 print("总共需支付票价{}".format(num * price)) elif self.day == "周六" or "周日": if self.discount: price = 88 else: price = 110 print("总共需支付票价{}".format(num * price))
修正后的代码如下:
```
class Ticket:
def __init__(self, day, discount=False):
self.day = day
self.discount = discount
def get_ticket_price(self, num):
if self.day == "周一":
print("博物馆周一闭馆,不提供售票服务")
elif self.day in ["周二", "周三", "周四", "周五"]:
if self.discount:
price = 80
else:
price = 100
print("总共需支付票价{}".format(num * price))
elif self.day in ["周六", "周日"]:
if self.discount:
price = 88
else:
price = 110
print("总共需支付票价{}".format(num * price))
```
修改的内容如下:
- 在第8行,将 `elif self.day == "周二" or "周三" or "周四" or "周五"` 改为 `elif self.day in ["周二", "周三", "周四", "周五"]`,因为原来的表达式会被解释为 `elif (self.day == "周二") or ("周三") or ("周四") or ("周五")`,这样的结果是始终为 `True`,因为非空字符串被视为 `True`。
- 在第13行,将 `elif self.day == "周六" or "周日"` 改为 `elif self.day in ["周六", "周日"]`,原因同上。
#日历程序 Lunar=(1,3,5,7,8,10,12) def IsleapYear(year): flag =False if(year%4==0 and year%100!=0) or year%400==0: flag = True return flag def calculation(year,month): sum = 0 s_year=1990 while s_year < year-1: s_year +=1 if IsleapYear(s_year): sum+=366 else: sum+=365 s_month=1 while s_month<month: if s_month in Lunar: sum += 31 elif s_month==2: if IsleapYear(year): sum+=29 else: sum+=28 else: sum+=30 s_month+=1 return sum def display(sum,year,month): week=(sum+1)%7 if month in Lunar: day=31 elif month==2: if IsleapYear(year): day=29 else: day=28 else: day=30 print("日 一 二 三 四 五 六") count = 0 space = 0 while space<=week: space+=1 count+=1 print(" ",end="") if count%7==0: print(" ",end="") days=1 while days<=day: print(days," ",end="") days+=1 count+=1 if count %7 ==0: print("") def main(): year =int( input("输入年:")) if year<1990 or year>9999: print("year输入错误") main() month = int(input("输入月:")) if month<1 or month > 12: print("month输入错误") main() sums=calculation(year,month) display(sums,year,month) if __name__=="__main__": while True: main() print() choose=input("是否继续:(y/n)") if choose in("n","N"): break怎么用C语言表示
以下是用 C 语言实现该日历程序的示例代码:
#include <stdio.h>
int IsLeapYear(int year) {
int flag = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = 1;
}
return flag;
}
int Calculation(int year, int month) {
int sum = 0;
int s_year = 1990;
while (s_year < year - 1) {
s_year++;
if (IsLeapYear(s_year)) {
sum += 366;
} else {
sum += 365;
}
}
int s_month = 1;
while (s_month < month) {
if (s_month == 1 || s_month == 3 || s_month == 5 || s_month == 7 || s_month == 8 || s_month == 10 || s_month == 12) {
sum += 31;
} else if (s_month == 2) {
if (IsLeapYear(year)) {
sum += 29;
} else {
sum += 28;
}
} else {
sum += 30;
}
s_month++;
}
return sum;
}
void Display(int sum, int year, int month) {
int week = (sum + 1) % 7;
int day;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
day = 31;
} else if (month == 2) {
if (IsLeapYear(year)) {
day = 29;
} else {
day = 28;
}
} else {
day = 30;
}
printf("日 一 二 三 四 五 六\n");
int count = 0;
int space = 0;
while (space <= week) {
space++;
count++;
printf(" ");
if (count % 7 == 0) {
printf(" ");
}
}
int days = 1;
while (days <= day) {
printf("%-3d ", days);
days++;
count++;
if (count % 7 == 0) {
printf("\n");
}
}
}
int main() {
int year, month;
char choose;
do {
printf("输入年:");
scanf("%d", &year);
if (year < 1990 || year > 9999) {
printf("year输入错误\n");
continue;
}
printf("输入月:");
scanf("%d", &month);
if (month < 1 || month > 12) {
printf("month输入错误\n");
continue;
}
int sums = Calculation(year, month);
Display(sums, year, month);
printf("\n是否继续:(y/n)");
scanf(" %c", &choose);
} while (choose == 'y' || choose == 'Y');
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)