【问题标题】:I'm getting an "Invalid syntax error" on this portion of my code我的这部分代码出现“无效语法错误”
【发布时间】:2022-10-07 09:48:52
【问题描述】:

我对编程相当陌生,并且在我的第一堂课的期末考试中,但我遇到了这个问题,我似乎无法弄清楚为什么。我在该行使用的语法是有效的语法,我查看了上面的行并没有发现任何问题,所以我不确定为什么会出现此错误。

#Prompt user for # of rooms

print(\"Hello! Please follow the prompts on screen for the services listed:\\n\")
numRooms = eval(input(\'How many rooms will be cleaned? Note: No more than 10 rooms!\\n\'))

while(numRooms > 10 or <= 0):
    print(\"***Invalid number of rooms***\\n\")
    numRooms = eval(input(\'How many rooms will be cleaned? Note: No more than 10 rooms!\\n\'))

(另外,第一次使用堆栈溢出,所以如果我表现得无知,我很抱歉)

  • 如果这是决赛,那么您确定您可以在 Stack Overflow 上寻求帮助吗?
  • 我不明白为什么不,我会联系教授,但他并没有真正回应。我查看了学习资源,并自行研究了它,但无济于事。
  • while(numRooms &gt; 10 or &lt;= 0) 不是有效的 Python 语法。

标签: python syntax-error


【解决方案1】:

这是您的代码的正确语法。您在 if 块中犯了语法错误

print("您好!请按照屏幕上的提示获取所列服务:\n") numRooms = eval(input('要清理多少个房间?注意:不超过10个房间!\n'))

while(numRooms > 10 or numRooms<= 0):
    print("***Invalid number of rooms***\n")
    numRooms = eval(input('How many rooms will be cleaned? Note: No more than 10 rooms!\n'))

【讨论】:

  • 这段代码也一样坏了,至少问题中的代码格式正确
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多