请出几道python中结合if...else和input()函数的练习题
时间: 2024-04-16 20:27:38 浏览: 21
当然,下面是几道结合if...else和input()函数的Python练习题:
题目1:编写一个程序,要求用户输入一个整数,判断该数是奇数还是偶数,并输出相应的结果。
```python
num = int(input("请输入一个整数:"))
if num % 2 == 0:
print("这是一个偶数")
else:
print("这是一个奇数")
```
题目2:编写一个程序,要求用户输入一个年份,判断该年份是否为闰年,并输出相应的结果。闰年的判断规则为:能被4整除但不能被100整除,或者能被400整除的年份都是闰年。
```python
year = int(input("请输入一个年份:"))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("这是一个闰年")
else:
print("这不是一个闰年")
```
题目3:编写一个程序,要求用户输入三个数,判断其中的最大数,并输出最大数。
```python
num1 = float(input("请输入第一个数:"))
num2 = float(input("请输入第二个数:"))
num3 = float(input("请输入第三个数:"))
if num1 >= num2 and num1 >= num3:
print("最大数为:", num1)
elif num2 >= num1 and num2 >= num3:
print("最大数为:", num2)
else:
print("最大数为:", num3)
```
这些练习题涉及了if...else语句和input()函数的使用,希望对你有帮助!如果你还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_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)