一个3位正整数,每位上的数字的3次幂之和等于它本身,这类数称为水仙花数。小芸编写Python程序输出100至999之间的所有水仙花数,代码如下
1 for i in range(100,1000):
2 s=str(i)
3 bai=s[0]
4 shi=s[1]
5 ge=s[2]
6 if int(s)==bai**3+shi**3+ge**3:
7 print(i,"是水仙花数")
程序调试时报错,请分析程序出错的原因( )
>>>%Run‘水仙花数.py'
Traceback(most recent call last):
File"D:\KINGSTON\123\水仙花数.py",line 6,in<module>
If int(s)==bai**3+shi**3+ge**3:
TypeError:unsupported operand type(s) for** or pow():'str' and 'int’
1 for i in range(100,1000):
2 s=str(i)
3 bai=s[0]
4 shi=s[1]
5 ge=s[2]
6 if int(s)==bai**3+shi**3+ge**3:
7 print(i,"是水仙花数")
程序调试时报错,请分析程序出错的原因( )
>>>%Run‘水仙花数.py'
Traceback(most recent call last):
File"D:\KINGSTON\123\水仙花数.py",line 6,in<module>
If int(s)==bai**3+shi**3+ge**3:
TypeError:unsupported operand type(s) for** or pow():'str' and 'int’
A.第2行不能使用str()函数把变量i的值转换为字符串类型 |
B.第6行的幂运算不能使用**运算符,应该使用*运算符 |
C.第6行的bai、shi、ge三个变量是字符串类型,不能进行幂运算 |
D.第7行输出格式错误,应该使用双引号 |
更新时间:2024-04-02 21:44:32
|
【知识点】 Python语言基础
相似题推荐
选择题
|
适中
(0.65)
【推荐1】在Python中,print("12"+"3")的运行结果为( )
A.15 | B."15" | C.123 | D."123" |
您最近一年使用:0次
选择题
|
适中
(0.65)
名校
【推荐2】下列 Python 代码运行结果是( )。
a = 3
b = 4
a = a*b
print(a*b)
a = 3
b = 4
a = a*b
print(a*b)
A.3 | B.4 | C.12 | D.48 |
您最近一年使用:0次