p121: *题4.1:猜数字游戏.在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了!”:小于预设的数,显示“遗憾,太小了!”,如此循环,直至猜中该数,显示“预测N次,你猜中了!”,其中N是用户输入数字的次数. 源代码: from random import*y=5n=1m=Falsewhile(m==False): x=eval(input("请输入您猜想的数字:")) try: if(x>y):
p56: *2.1 实例1的修改.改造实例代码1.1,采用eval(input(<提示内容>))替换现有输入部分,并使输出的温度值为整数. 源代码: TempStr=input("请输入符号:") x=eval(input("请输入温度值:")) if TempStr in ['F','f']: C=(x-32)/1.8 print("转换后的温度是{:.2f}C".format(C)) elif TempStr in
tuple 元组(创建后不能修改) tuple = "cat","dog","tiger","human" print(tuple) def fun(x): return x,x*2 print(type(fun(3))) for x,y in ((2,0),(2,5),(3,8)): print(x+y) set 集合 S = {435,"BAT",(25,"CS"),435} pri
lambda sum = lambda x,y : x + y print(sum(3,3),type(sum)) 默认参数 def prints(str,times = 2) : print(str*times) prints("Tree",3) 变长参数 def sums(*a): res = 0 for i in a: res += i return res print(sums(1,2,3,4,5)) return def swap(a,b): return b,a print
天天向上 dayup,dayfactor = 1.0,0.01 for i in range(365): if i % 7 in [6,0]: dayup = dayup*(1-dayfactor) else: dayup = dayup*(1+dayfactor) print("{:.2f}".format(dayup)) 天天向上2 def dayUP(df): dayup = 1.0 for i in range(365): if i % 7 in [6,0]: dayup =
温度转换 def tempConvert(ValueStr): if ValueStr[-1] in ['F','f']: ans = (eval(ValueStr[0:-1]) - 32)/1.8 print("{:.2f}C".format(ans)) elif ValueStr[-1]=='C' or ValueStr[-1]=='c': ans = 1.8*eval(ValueStr[0:-1]) + 32 print("{:.2f}F".format(an
Everybody in this country should learn how to program a computer,because it teaches you how to think. ——史蒂夫~乔布斯 圆的面积 import math radius = 25 area = math.pi*radius*radius print(area) print("{:.2f}".format(area)) 输入 name = input("输入人名:")