Python 条件判断和函数入门
程序员文章站
2022-05-30 10:22:26
...
一门编程语言中基本的单元就是函数,基本的结构就是条件判断循环等等。
今天简单分析一下条件判断与函数初步。
#coding=utf-8
print("this is a varchar")
# 1、if 语句嵌套
month = int(input("please enter a month: "))
if 1 <= month <= 12:
print("input month correctly")
if month <= 3:
print("spring")
elif month <= 6:
print("summer")
elif month <= 9:
print("autumn")
else:
print("winter")
else:
print("input error")
# 如果输入字符串等非数字就会报错
# 2 if-else
# 表达式1 if 判断 else 表达式2
money = int(input("please input price"))
pay = money - 20 if money >= 100 else money
print(pay)
# 如果价格超过100元,那么总价减20元。
# 3 输入
money = input("please input money")
moneyNew = int(money)
print(moneyNew)
# pass 空语句
pass
# 循环语句
# no parameter
def function():
print("this is a function")
a = 1 + 2
print(a)
# if you create a function, the function can't run by itself. You should run it.
function()
# two parameters
def func(a, b):
c = a + b
d = str(c)
print("this is sum " + d)
func(1, 2)
上一篇: MySQL学习4:数据的增删改查CRUD