从零开始的python之旅(day2)
今天主要学数据类型,类型处理方式和循环以及异常处理(当然还有数学。目前学到现在,我感觉python和c语言最大的区别就是,python更更更方便了,主要是前人栽树后人乘凉了,特别是对于元组类型和列表类型以及字符串处理中,python和c语言有相似,但是python更好读而且还更简洁。
month = 'JanFebMarAprMayJunJulAugSepOctNovDec'
ch = input('请输入月份[1~12}\n')
print('月份为'+month[(int(ch) - 1)*3:int(ch)*3])
月份匹配
在这一段使用中,会发现如果利用python写会简单很多(当然不是无脑吹哈,可能是新学语言的滤镜
import math
import random
import time
k = 10000000
hits = 0
start_time = time.perf_counter()
for i in range(1, k):
x, y = random.random(), random.random()
dist = math.sqrt(x**2 + y**2)
if dist <= 1:
hits = hits + 1
pi = 4 * hits / k
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print("Π的值为 {0:.6f}".format(pi))
print("所需时间为 {0:.6f} 秒".format(elapsed_time))
计算Π值
嗷嗷这个方法还是第一次听说,见识少了
import sys
n = int(input("请输入n\n"))
max = -sys.maxsize
for i in range(n):
x = int(input("请输入数\n"))
if(max < x):
max = x
print(max)
获取n个数中最大的那个数
import math
def main():
try:
x,y,c = eval(input('please enter the x,y,c\n'))
delt = math.sqrt(y ** 2 - 4 * x * c)
ans1 = (-y - delt) / 2 / x
ans2 = (-y + delt) / 2 / x
print(ans1,ans2)
except ValueError as obj:
if str(obj) == 'math domain error':
print('\nNo Real Root')
else:
print("\nYou didn't give me the right number of coefficients")
except TypeError:
print("\nYou didn't enter enough number")
except NameError:
print("\nYou didn't enter number")
except SyntaxError:
print("\nYou didn't enter a correct form")
except Exception as e:
print("\nSomething is Wrong {}".format(e))
finally:
print("\nFinish")
main()
异常处理
随笔记得很糙哈,但是,慢慢理解肯定慢慢越做越好的,加油吧
标签:python,random,day2,except,从零开始,print,import,math From: https://www.cnblogs.com/Liyukio/p/18669458