python if 练习
时间: 2023-07-24 19:15:19 浏览: 53
以下是一些Python if else用法的练习题:
1. 编写一个程序,输入一个数字,如果它是正数,则输出“这是一个正数”,否则输出“这是一个负数”。
2. 编写一个程序,输入两个数字,如果它们相等,则输出“这两个数字相等”,否则输出“这两个数字不相等”。
3. 编写一个程序,输入一个年份,如果它是闰年,则输出“这是一个闰年”,否则输出“这不是一个闰年”。
4. 编写一个程序,输入一个数字,如果它是偶数,则输出“这是一个偶数”,否则输出“这是一个奇数”。
5. 编写一个程序,输入一个字母,如果它是元音字母,则输出“这是一个元音字母”,否则输出“这是一个辅音字母”。
希望这些练习题能够帮助你更好地理解Python if else用法。
相关问题
python if else练习题
好的,以下是 Python if else 练习题:
1. 编写一个程序,判断一个数是否为偶数。
```python
num = int(input("请输入一个整数:"))
if num % 2 == 0:
print(num, "是偶数")
else:
print(num, "不是偶数")
```
2. 编写一个程序,判断一个年份是否为闰年。
```python
year = int(input("请输入一个年份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "是闰年")
else:
print(year, "不是闰年")
```
3. 编写一个程序,输入三个数,输出其中最大的数。
```python
a = float(input("请输入第一个数:"))
b = float(input("请输入第二个数:"))
c = float(input("请输入第三个数:"))
if a >= b and a >= c:
print(a, "是最大的数")
elif b >= a and b >= c:
print(b, "是最大的数")
else:
print(c, "是最大的数")
```
4. 编写一个程序,输入三个数,输出其中最小的数。
```python
a = float(input("请输入第一个数:"))
b = float(input("请输入第二个数:"))
c = float(input("请输入第三个数:"))
if a <= b and a <= c:
print(a, "是最小的数")
elif b <= a and b <= c:
print(b, "是最小的数")
else:
print(c, "是最小的数")
```
5. 编写一个程序,输入一个字符,判断它是大写字母、小写字母还是数字。
```python
ch = input("请输入一个字符:")
if ch.isupper():
print(ch, "是大写字母")
elif ch.islower():
print(ch, "是小写字母")
elif ch.isdigit():
print(ch, "是数字")
else:
print(ch, "不是大写字母、小写字母或数字")
```
6. 编写一个程序,输入一个年龄,判断它是否为儿童(0-12岁)、青少年(13-18岁)或成年人(19岁及以上)。
```python
age = int(input("请输入一个年龄:"))
if age < 0:
print("输入有误")
elif age <= 12:
print("儿童")
elif age <= 18:
print("青少年")
else:
print("成年人")
```
7. 编写一个程序,输入一个月份,输出该月份的天数。
```python
month = int(input("请输入一个月份:"))
if month in [1, 3, 5, 7, 8, 10, 12]:
print(month, "月有31天")
elif month in [4, 6, 9, 11]:
print(month, "月有30天")
elif month == 2:
year = int(input("请输入一个年份:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(month, "月有29天")
else:
print(month, "月有28天")
else:
print("输入有误")
```
python if语句练习题
好的,我已经理解了您的问题。关于 Python 的 if 语句练习题,我可以提供一个简单的示例:
```
# 输入一个数字,如果它小于 10,输出“这是一个小数字”,否则输出“这是一个大数字”
num = int(input("请输入一个数字:"))
if num < 10:
print("这是一个小数字")
else:
print("这是一个大数字")
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)