1、输入包含3个字符的字符串,如果字符串不是3个字符,输出“输入数据出错。”;否则,计算其中数字字符的和。比如:输入‘1a3’,则输出4。
2、随机生成3个1000以内的实数(保留3位小数),输出其中的最大值。如:
29.334 , 0.865 , 386.398
最大值是:386.398
1.
s = input("请输入一个3个字符的字符串:")
if len(s) != 3:
print("输入数据出错。")
else:
total = 0
for ch in s:
if ch.isdigit():
total += int(ch)
print(total)
2.
import random
a = round(random.uniform(0, 1000), 3)
b = round(random.uniform(0, 1000), 3)
c = round(random.uniform(0, 1000), 3)
print(f"{a}, {b}, {c}")
max_val = max(a, b, c)
print(f"最大值是:{max_val:.3f}")
string = input("请输入一个包含3个字符的字符串:")
if len(string) != 3:
print("输入数据出错。")
else:
sum = 0
for ch in string:
if ch.isdigit():
sum += int(ch)
print("数字字符的和为:", sum)
import random
num1 = round(random.uniform(0, 1000), 3)
num2 = round(random.uniform(0, 1000), 3)
num3 = round(random.uniform(0, 1000), 3)
print(num1, num2, num3)
print("最大值是:", max(num1, num2, num3))