Demo55
**
程序编辑:
# 提示用户输入一个十进制整数
number = int(input("Enter a number:"))
def decToHex(number):
hexStr = ""
while number != 0:
temp = number % 16
if temp < 10:
hexStr = str(temp) + hexStr
else:
hexStr = chr(55 + temp) + hexStr
number //= 16 # number = number // 16
return hexStr
hexStr = decToHex(number)
print("十进制数%d,对应的十六进制数是%s"%(number,hexStr))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/55.py
Enter a number:31
十进制数31,对应的十六进制数是1F
Process finished with exit code 0
Demo56
Python (求一个整数各个数字的和)编写一个函数,计算一个整数各个数字的和。使用下面的函数头:
def sumDigits(n):
例如: sumDigits(234) 返回9 (2+3+4)。(提示:使用求余运算符%提取数字,而使用除号//去掉提取出来的数字。例如:使用234%10(=4)抽取4.然后使用234//10(=23)从234中去掉4。使用一个循环来反复提取和去掉每个数字,直到所有数字被提取完为止。)编写程序提示用户输入一个整数,然后显示这个整数所有数字的和。
程序编辑:
number = int(input("Enter a number:"))
"""
123456 % 10 -> 6
12345 % 10 -> 5
1234
...
1 % 10 -> 1
0
"""
def sumDigits(n):
sum = 0
while n != 0:
sum += n % 10
n = n // 10
return sum
print(sumDigits(number))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/56.py
Enter a number:12344
14
Process finished with exit code 0
Demo57
**
**
#Return the reversal of an integer, e.g. reverse(456) returns
#654
def reverse(rumber):
#Return true if number is a palindrome
def isPalindrome (number):
使用函数reverse实现isPalindrome。如果一个数的反向数和它的顺向数一样,那么这个数就被称为回文数。编写一个测试程序,提示用户输入一个整数,然后输出这个整数是不是回文数。
程序编辑:
number = int(input("Enter a number:"))
def isPalindrome(number):
revNum = reverse(number)
return number == revNum
def reverse(number):
revNum = 0
while number != 0:
revNum = revNum * 10 + number % 10
number //= 10
return revNum
print(isPalindrome(number))
"""
123 % 10 3
12 % 10 2
1 % 1
0
int("321")
0*10 + 3 =3
3*10 + 2 =32
32*10 + 1 =321
"""
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/57.py
Enter a number:12321
True
Process finished with exit code 0
Demo58
**
**
def reverse(number):
例如: reserse(3456) 显示6543。编写一个测试程序,提示用户输入一个整数,然后显示它
的反向数。
程序编辑:
number = int(input("Enter a number:"))
def reverse(number):
revNum = 0
while number != 0:
revNum = revNum * 10 + number % 10
number //= 10
return revNum
print(reverse(number))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/58.py
Enter a number:1234987
7894321
Process finished with exit code 0
Demo59
**
程序编辑:
# Converts from Celsius to Fahrenheit
def celsiusToFahrenheit(celsius):
Fahrenheit = (9 / 5) * celsius +32
return Fahrenheit
# Converts from Fahrenheit to Celsius
def fahrenheitToCelsius(fahrenheit):
celsius = (5 / 9) * (fahrenheit - 32)
return celsius
print("Celsius\t\tFahrenheit\t\tFahrenheit\tCelsius")
i = 40
for j in range(120, 20, -10):
print(i,end = "\t\t\t")
print("%.1f"%(celsiusToFahrenheit(i)),end ="\t\t\t")
print(j, end="\t\t\t")
print("%.2f"%(fahrenheitToCelsius(j)))
i -= 1
if i == 30:
break
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/59.py
Celsius Fahrenheit Fahrenheit Celsius
40 104.0 120 48.89
39 102.2 110 43.33
38 100.4 100 37.78
37 98.6 90 32.22
36 96.8 80 26.67
35 95.0 70 21.11
34 93.2 60 15.56
33 91.4 50 10.00
32 89.6 40 4.44
31 87.8 30 -1.11
Process finished with exit code 0
Demo60
**
程序编辑:
# 数据:行数i 行数的值
# 注意:每次计算完每一行的值后,清空值 sum = 0
def m(i):
sum = 0
print(i, end="\t\t\t\t")
for i in range(1,i+1):
sum += i / (i + 1)
return sum
print("i\t\t\t\tm(i)")
for i in range(1, 21):
print("%.4f"%m(i))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/60.py
i m(i)
1 0.5000
2 1.1667
3 1.9167
4 2.7167
5 3.5500
6 4.4071
7 5.2821
8 6.1710
9 7.0710
10 7.9801
11 8.8968
12 9.8199
13 10.7484
14 11.6818
15 12.6193
16 13.5604
17 14.5049
18 15.4523
19 16.4023
20 17.3546
Process finished with exit code 0
Demo61
**
**
#Returns true if the sum of any two sides is
#greater than the third side.
def isValid(side1, side2, side3):
#Returns the area of the triangle.
def area(side1, side2, side3):
编写一个测试程序,读入三角形三边的值,若输人有效则计算面积。否则,显示输人无效。
计算三角形面积的公式在编程题2.14中给出。下面是一些示例运行。
Enter three sides in double: 1,3, 1
Input is invalid
Enter three sides in doub1e: 1,1, 1
The area of the triangle is 0.4330127018922193
程序编辑:
# 数据:三条边,面积
# 步骤:
# 1.输入三条边
# 2.判断是不是一个三角形
# 3.计算三角形面积,输出值
def k(a,b,c):
if a + b > c and a + c > b and b + c > a:
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
else:
area = "Input is invalid"
return area
a,b,c = eval(input("Enter three sides in double:"))
print("The area of the triangle is ",k(a,b,c))
# 方法二:
def isValid(s1, s2, s3):
return s1 + s2 > s3 and s2 + s3 > s1 and s1 + s3 > s2
def area(s1, s2, s3):
s = (s1 + s2 + s3) / 2
return (s * (s - s1) * (s - s2) * (s - s3)) ** 0.5
s1, s2, s3 = eval(input("Enter three sides in double:"))
if isValid(s1, s2, s3):
print("area is %.2f" % area(s1, s2, s3))
else:
print("Invalid")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day04/61.py
Enter three sides in double:1,1,1
The area of the triangle is 0.4330127018922193
Enter three sides in double:1,1,1
area is 0.43
Process finished with exit code 0