38 lines
758 B
Python
38 lines
758 B
Python
# 练习 1:判断正负数
|
||
# 输入一个数字,判断它是正数还是负数
|
||
num = -3
|
||
|
||
result = "正数" if num > 0 else "负数"
|
||
print(result)
|
||
|
||
# 练习 2:找两个数中的较大者
|
||
a = 10
|
||
b = 20
|
||
|
||
max_value = a if a > b else b
|
||
print(f"较大值是: {max_value}")
|
||
|
||
# 练习 3:判断是否为偶数
|
||
n = 7
|
||
|
||
result = "偶数" if n % 2 == 0 else "奇数"
|
||
print(f"{n} 是 {result}")
|
||
|
||
# 练习 4:嵌套三目运算符(判断分数等级)
|
||
score = 85
|
||
|
||
grade = "优秀" if score >= 90 else ("良好" if score >= 75 else "及格")
|
||
print(f"成绩等级: {grade}")
|
||
|
||
# 练习 5:用户输入进行判断
|
||
num = int(input("请输入一个整数:"))
|
||
result = "正数" if num > 0 else ("零" if num == 0 else "负数")
|
||
print(f"{num} 是 {result}")
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|