Discuss / Python / 请教哪里出错了?

请教哪里出错了?

Topic source

不歸了

#1 Created at ... [Delete] [Delete and Lock User]

name = input ('请输入你的名字:') print('Hello,', name,'现在开始考试\n题目一:1024768=') answer = input ('请输入你的答案:') s = (1024768) if answer == s : print('Your answer is Right') else : print('Your answer is Wrong.The right answ is',s)

不知道为什么即使输入答案是正确的,最后显示都是“Your answer is wrong...

数据类型不同。input保存的是string类型。

zaytllen

#3 Created at ... [Delete] [Delete and Lock User]

朋友你好,正确的代码已经给出。 错的原因是“==”比较的是两个“字符串”是否一致 而你开始的s其实是一个数字,输入框输入的答案是按照字符串处理的,所以要把s也转换做字符串才好比较。虽然不转换样子看起来不会有差,但是计算机处理起来是不一样的 另外,个人之前有接触过java,知道“==”比较的是两个字符串的内存位置,要想比较字面一致可能是调用equals()函数比较合适,但是这个就得另外说了····希望我的回答能够对你有帮助

name = input ('请输入你的名字:')
print('Hello,', name,'现在开始考试\n题目一:1024768=')
answer = input ('请输入你的答案:')
s = "1024768"//你这里错了,1024768记得用引号引起来
if answer==s :
     print('Your answer is Right')
else :
     print('Your answer is Wrong.The right answ is',s)

不歸了

#4 Created at ... [Delete] [Delete and Lock User]

明白了,将answer = input()改为answer = int(input())就可以了


  • 1

Reply