嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=10211084839195730432#page=25 中的30-34讲

#coding=gbk
#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=10211084839195730432#page=25 中的30-34讲
# 文件循环
"""
def main():
  fileName = input("What file are the numbers in? ")
  infile = open(fileName,'r')
  sum = 0
  count = 0
  line = infile.readline()
  while line != "":
    for xStr in line.split(","):
      sum = sum + eval(line)
      count += 1
    line = infile.readline()
  print(" The average of the numbers is ", sum / count)

main()
"""
# 后测循环实现
# Python没有后测循环语句,但可以通过while间接实现,实现举例:
"""
number = -1
while number < 0:
  number = eval(input("Enter a positive number: "))
"""
"""
while True:
  number = eval(input("Enter a positive number: "))
  if x >= 0:break
"""
"""
number = -1
while number < 0:
  number = eval(input("Enter a positive number: "))
  if number < 0:
    print("The number you entered was not positive.")   #增加输入提示
"""
"""
while True:
  number = eval(input("Enter a positive number: "))
  if x >= 0:
    break
  else:
    print("The number you entered was not positive.")
"""
# 布尔表达式
# 条件语句和循环语句都使用布尔表达式作为条件;
# 布尔值为真或假,以True和False表示
#
# 布尔操作符优先级:not > and > or
#
# 布尔代数
# 布尔表达式遵循特定的代数定律,这些规律被称为布尔逻辑或布尔代数。
# a and false == false
# a and true == a
# a or false == a
# a or true == true
# and 和 or 操作符都符合分配定律
# a or (b and c) == (a or b) and (a or c)
# a and (b or c) == (a and b) or (a and c)
# 德摩根定律,not放进表达式后,and 和 or 运算符之间发生的变化。
# not(a or b) == (not a) and (not b)
# not(a and b) == (not a) or (not b)
# 布尔表达式作为决策
# 对于数字(整形和浮点型)的零值被认为是false,任何非零值都是True
# bool类型仅仅是一个特殊的整数,可以通过表达式True + True的值来测试一下。
# 空的序列在布尔类型中是False,非空序列为True
# 布尔表达式思考
# x and y:如果x是假,输出x,否则输出y
# x or y :如果x为真,输出x,否则输出y
# not x :如果x为假,输出True,如果x为真,输出False #只输出True或False
#
#
# 函数定义
# 函数名<name>:任何有效的Python标识符
# 参数列表<parameters>:调用函数时传递纵它的值
# 参数个数大于等于零
# 多个参数用逗号分隔
# 形式参数:定义函数时,函数名后面圆括号中的变量,简称形参,形参只在函数内部有效。
# 实际参数:调用函数时,函数名后面圆括号中的变量,简称实参
#
# 函数的返回值
# return语句:程序退出该函数,并返回到函数被调用的地方
# return语句返回的值传递给调用程序
# 返回值有两种形式:
# 返回一个值
# 返回多个值
# 无返回值的return 语句等价于 return None
# None是表示没有任何东西的特殊类型。
# 返回值可以是一个变量,也可以是一个表达式。
"""
def sumDiff(x,y):
  sum = x + y
  diff = x - y
  return sum,diff #return可以有多个返回值

s,d = sumDiff(1,2)
print("The sum is ",s,"and the diff is ",d)
"""
#判断是否是三角形的函数
import math

def square(x):
  return x * x

def distance(x1,y1,x2,y2):
  dist = math.sqrt(square(x1-x2) + square(y1-y2))
  return dist

def isTriangle(x1,y1,x2,y2,x3,y3):
  flag = ((x1-x2)*(y3-y2)-(x3-x2)*(y1-y2)) != 0
  return flag

def main():
  print("请依次输入三个点的坐标(x,y)")
  x1, y1 = eval(input("坐标点1:(x,y)= "))
  x2, y2 = eval(input("坐标点2:(x,y)= "))
  x3, y3 = eval(input("坐标点3:(x,y)= "))
  if (isTriangle(x1, y1, x2, y2, x3, y3)):
    perim = distance(x1, y1, x2, y2)+distance(x2, y2, x3, y3)+distance(x1, y1, x3, y3)
    print("三角形的周长是:{0:0.2f}".format(perim))
  else:
    print("输入坐标不能构成三角形。")

main()

原文地址:https://www.cnblogs.com/fengbo1113/p/7793469.html