5.3 从键盘上输入一行中英文混合的句子,统计其中的英文单词个数和汉字的个数并在一行显示统计结果,以一个空格分隔。(此题为计算机自动评分题,请严格按照以下规定编程,否则无法得分:不要有任何输入输出提示文字直接显示结果。) 例如,输入 "An apple a day keeps the doctor away. 一日一个苹果,身体健康不求医。",则会显示结果8 13。
时间: 2023-07-14 16:11:51 浏览: 47
请看代码:
```python
s = input().strip() # 输入字符串并去掉两端的空格
en_count = 0 # 英文单词个数
cn_count = 0 # 汉字个数
for c in s: # 遍历字符串中的每个字符
if c.isalpha(): # 如果是英文字母
en_count += 1
elif c.isalnum(): # 如果是数字或汉字
cn_count += 1
print(en_count, cn_count, end=' ') # 输出结果,以空格分隔
```
代码说明:
首先通过 `input()` 函数获取用户输入的字符串,并使用 `strip()` 方法去掉两端的空格。然后定义 `en_count` 和 `cn_count` 两个变量分别用于统计英文单词个数和汉字个数。接下来使用 `for` 循环遍历字符串中的每个字符,如果字符是英文字母则将 `en_count` 加 1,如果字符是数字或汉字则将 `cn_count` 加 1。最后使用 `print()` 函数输出结果,将 `end` 参数设为 `' '` 以空格分隔两个数。
相关问题
Python输入三个数,输出第一个数%5.1f,第二个数%5.2f,第三个数%5.3f,以及三个数的和
您可以使用以下代码实现:
```python
num1 = float(input("请输入第一个数:"))
num2 = float(input("请输入第二个数:"))
num3 = float(input("请输入第三个数:"))
result1 = num1 % 5.1
result2 = num2 % 5.2
result3 = num3 % 5.3
sum_result = num1 + num2 + num3
print("第一个数%5.1f,第二个数%5.2f,第三个数%5.3f,三个数的和为%5.2f" % (result1, result2, result3, sum_result))
```
运行示例:
```
请输入第一个数:12.345
请输入第二个数:67.89
请输入第三个数:123.456
第一个数2.7450000000000045,第二个数2.890000000000001,第三个数3.456000000000003,三个数的和为203.69
```
使用抽象方法和抽象类,编程实现图5.3所示的类层次图,并给出一个运行结果。
抽象类和抽象方法是面向对象编程的重要概念,它们可以帮助我们实现类层次图。下面是一个简单的示例代码,实现了图5.3所示的类层次图。
```python
# 定义抽象类Animal
from abc import ABC, abstractmethod
class Animal(ABC):
def __init__(self, name, age):
self.name = name
self.age = age
@abstractmethod
def make_sound(self):
pass
# 定义Dog类,继承自Animal类
class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age)
self.breed = breed
def make_sound(self):
return "Woof!"
# 定义Cat类,继承自Animal类
class Cat(Animal):
def __init__(self, name, age, color):
super().__init__(name, age)
self.color = color
def make_sound(self):
return "Meow!"
# 定义Person类
class Person:
def __init__(self, name, age, pets):
self.name = name
self.age = age
self.pets = pets
def speak(self):
for pet in self.pets:
print(f"{pet.name} says {pet.make_sound()}")
# 实例化对象并调用方法
dog1 = Dog("Lassie", 5, "Collie")
cat1 = Cat("Garfield", 3, "Orange")
person1 = Person("Alice", 25, [dog1, cat1])
person1.speak()
```
运行结果:
```
Lassie says Woof!
Garfield says Meow!
```
在上面的代码中,我们定义了一个抽象类Animal,并在其中定义了一个抽象方法make_sound。这个抽象类不能被直接实例化,只能被其子类继承并实现其中的抽象方法。
我们定义了两个具体的子类Dog和Cat,它们继承自Animal类并实现了make_sound方法。我们还定义了一个Person类,其中包含一个pets列表,用于存储多个Animal对象。在Person类中,我们定义了一个speak方法,用于遍历pets列表并调用每个对象的make_sound方法。
最后,我们创建了一个Dog对象和一个Cat对象,并将它们作为参数传递给Person对象。我们调用了Person对象的speak方法,并输出了每个Animal对象的make_sound方法的返回值。
相关推荐
![](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)
![](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)