Python课上的作业。
贴到这里,供大家参考哦~
欢迎交流指正,共同进步,欸嘿!
没有把全部练习题写完,仅写了要求的作业。
至于运行结果,大家可以自己去试试
# programp practice 4.1 猜数游戏
a = 8
i = 1
while 1:
b = eval(input(" please guess a number bewteen\
\r 0 and 9 that i am thinking and enter a n* : "))
if b < a :
print(" what a pity! it is too small!")
i = i+1
elif b > a:
print(" what a pity! it is too large")
i = i+1
else:
print(" now you are right after {} times".format(i))
break
# programp practice 4.2 统计不同字符个数
strs = input(" please enter anything you want: ")
a,b,c,d = 0,0,0,0
for i in strs:
if "0" <= i <= "9": # 判断数字
a += 1
elif i ==" " : # 判断空格
b += 1
elif "a" <= i <= "z" or "A" <= i <= "Z" : # 判断英文字符
c += 1
else: # 判断其他字符 会把中文加到这里来
d += 1
print("English characters have {},\n numbers have {},\
\n spaces have {},\n characters have {}".format(c,a,b,d))
# programp practice 4.2 统计不同字符个数 ,用unicode 与上面的本质一样
c,n,b,o = 0,0,0,0 # c代表字符个数 n代表数字个数 b代表空格个数 o代表其他字符个数
strs = input("please enter anything you want:")
for s in strs:
if ord('a') <= ord(s) <= ord('z') or ord('A') <= ord(s) <= ord('Z'):
c += 1
elif ord('0') <= ord(s) <= ord('9'):
n += 1
elif ord(' ') == ord(s):
b += 1
else:
o += 1
print("English word{0}个,number{1}个,space{2}个,other words{3}个".format(c,n,b,o))
# programp practice 4.4 猜数游戏续1
import random as r
a = r.randint(0,100)
b = eval(input("please enter a n* between 0 and 100 : "))
i = 1
while b != a :
i += 1
if b < a :
print("whta a pity! it is too small!")
elif b > a:
print("what a pity! it is too large")
else:
print("now you are right after {} times".format(i))
b = eval(input("please enter a n* between 0 and 100 : "))
else:
print("you toally entered {} times and the num is {}".format(i,b))
# programp practice 4.5 猜数字续2 - 成功例子
import random as r
num = r.randint(0,100)
i = 0
while 1 :
b = input("please enter a numbers bewteen 0 and 100 : ")
i += 1
if b.isdigit() : #判断字符串b是否为整数 isinstance(a,int)
b = eval(b)
if b < num :
print("sorry! it is too small!")
elif b > num :
print("sorry! it is too large!")
else:
print("u entered {} times and the number is {}".format(i,b))
break
else:
print("u must enter a n* !")
# programp practice 4.7 用异常处理改造实例1-循环直至正确输出
while 1 :
try:
tem = input("please enter temperature in N* with unit :")
if tem[:-1].isnumeric() : # 只能处理整数的温度
if tem[-1] in ["f","F"]:
C = (eval(tem[:-1]) - 32)/1.8
print('the result is {:.2f}C'.format(C))
elif tem[-1] in ['C','c']:
F = 1.8 * (eval(tem[:-1])) + 32
print('the result is {:.2f}F'.format(F))
break
else:
print("the content u entered is wrong")
except:
print("the format u entered is wrong")
上一个4.7 的做法只能处理整数,
下面这个可以处理小数哦
# programp practice 4.7 用异常处理改造实例1-循环直至正确输出
while 1 :
try: # 可处理小数的温度输入
tem = input("please enter temperature in N* with unit: ")
if tem[-1] in ["f","F"]:
C = (eval(tem[:-1]) - 32)/1.8
print('the result is {:.2f}C'.format(C))
break # 跳出循环
elif tem[-1] in ['C','c']:
F = 1.8 * (eval(tem[:-1])) + 32
print('the result is {:.2f}F'.format(F))
break # 跳出循环
else:
print("the content u entered is wrong")
except:
print("the format u entered is wrong")
就这样叭,码字不易,太难了