控制结构: (1)编写一个默认值参数day=Monday的名为DaySelect的函数。具体要求:输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。分别调用wednesday、Tuesday并返回相应的值。 (2)编写一个名为SearchRoot的函数,包含默认值参数a=1和形参b、c,用于实现求一元二次方程ax2+bx+c=0(a≠0)的根。任取三个实参a, b, c,并输出结果。
时间: 2024-03-20 16:44:35 浏览: 173
好的,以下是Python的代码实现:
```python
# DaySelect函数
def DaySelect(day="Monday"):
if day[0].lower() == "w":
return "Wednesday"
elif day[0].lower() == "t":
if len(day) > 1 and day[1].lower() == "u":
return "Tuesday"
elif len(day) > 1 and day[1].lower() == "h":
return "Thursday"
else:
return "Invalid input"
elif day[0].lower() == "f":
return "Friday"
elif day[0].lower() == "s":
if len(day) > 1 and day[1].lower() == "a":
return "Saturday"
elif len(day) > 1 and day[1].lower() == "u":
return "Sunday"
else:
return "Invalid input"
else:
return "Invalid input"
# SearchRoot函数
def SearchRoot(a=1, b=1, c=1):
if a == 0:
return "a不能为0"
else:
delta = b**2 - 4*a*c
if delta > 0:
x1 = (-b + delta**0.5) / (2*a)
x2 = (-b - delta**0.5) / (2*a)
return "该一元二次方程有两个实根:x1 = {:.2f}, x2 = {:.2f}".format(x1, x2)
elif delta == 0:
x = -b / (2*a)
return "该一元二次方程有一个实根:x = {:.2f}".format(x)
else:
return "该一元二次方程无实根"
```
使用示例:
```python
# 调用DaySelect函数
print(DaySelect("tue")) # 输出Tuesday
print(DaySelect("thu")) # 输出Thursday
print(DaySelect("f")) # 输出Friday
print(DaySelect("Sa")) # 输出Saturday
print(DaySelect("x")) # 输出Invalid input
# 调用SearchRoot函数
print(SearchRoot(2, 3, -1)) # 输出该一元二次方程有两个实根:x1 = 0.50, x2 = -1.00
print(SearchRoot(1, 2, 1)) # 输出该一元二次方程有一个实根:x = -1.00
print(SearchRoot(2, 1, 2)) # 输出该一元二次方程无实根
```
阅读全文