【问题标题】:Why does this code have an IndentationError? [duplicate]为什么这段代码有一个 IndentationError? [复制]
【发布时间】:2021-04-16 17:02:47
【问题描述】:

我正在尝试编写一个程序,其中用户输入一个 Int 值,然后必须使用 If 语句比较 int 并且必须打印结果,如果用户输入一个字符串,那么消息应该是“你已经输入一个字符串值”。

你能告诉我我的程序有什么问题吗

print ("Enter a value")
input()
if int (value) == 10:
print('The value is 10')
if (value) == str:       (Its wrong in here)
print (' Enter a Number not text')
elif int (value) == 12:
print('The value is not 10')
else: 
print('The value is not a number')
Error message        File "<ipython-input-35-29c9dd2647d4>", line 6
     print (' Enter a Number not text') IndentationError: expected an indented block

【问题讨论】:

  • 你知道缩进在 Python 中有多重要吗?你查过缩进是如何工作的吗?
  • 这就是你从一些word文件或pdf等复制粘贴代码时得到的。
  • 当我尝试在 stackoverflow 中添加我的帖子时,我无法以我原来的方式发布,所以我按原样发布了
  • print('输入一个数字:') input() if int(w) == 10: print('值为10') elif int(w) == 20: print('值为20') else: print('值为30')
  • @LokeshGovindaraju 在 cmets 中发布代码是没有用的,因为它摆脱了格式和空格。就像我说的那样,空格/缩进在 Python 中很重要。

标签: python if-statement conditional-statements double


【解决方案1】:
print("Enter a value")
value = input()
if not value.isnumeric():
    print("Enter a Number not text")
elif int (value) == 10:
    print("The value is 10") 
elif int (value) == 12: 
    print("The value is not 10")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2016-11-29
    • 2012-07-17
    • 2017-10-14
    相关资源
    最近更新 更多