mac终端错误,但不是VSCode的终端。

huangapple go评论120阅读模式
英文:

mac terminal error but not vscode's terminal

问题

当我在VSCode的终端中运行这个程序时,它可以正常工作,但是在我的Mac终端中运行时却出现了错误。

程序如下:

  1. def main():
  2. user_time = str(input("What time is it? "))
  3. # 将user_time转换为浮点数
  4. converted = convert(user_time)
  5. if converted <= 8 and converted >= 7:
  6. print("早餐时间!")
  7. elif converted <= 13 and converted >= 12:
  8. print("午餐时间!")
  9. elif converted <= 19 and converted >= 18:
  10. print("晚餐时间!")
  11. else:
  12. print("睡觉时间..")
  13. def convert(time):
  14. # 分割时间
  15. splitted = time.split(":")
  16. # 找到时间的小数值
  17. converted = int(splitted[0]) + (int(splitted[1])/60)
  18. # 返回给主程序
  19. return converted
  20. if __name__ == "__main__":
  21. main()

当我在Mac终端中运行它时,它显示以下错误:

  1. What time is it? 7:30
  2. Traceback (most recent call last):
  3. File "mealtime.py", line 30, in <module>
  4. main()
  5. File "mealtime.py", line 2, in main
  6. user_time = str(input("What time is it? "))
  7. File "<string>", line 1
  8. 7:30
  9. ^
  10. SyntaxError: invalid syntax

这个问题似乎是因为你在Mac终端中输入时间时使用了不正确的语法。在Mac终端中,你应该直接输入时间,而不是使用引号。尝试运行时输入7:30而不是"7:30",应该可以解决这个问题。

英文:

so i have this program written in vscode and when i run it in vscode's terminal, it works fine but not when i run it on my mac's terminal.

the program is as follows:

  1. def main():
  2. user_time = str(input(&quot;What time is it? &quot;))
  3. # converting user_time to float
  4. converted = convert(user_time)
  5. if converted &lt;= 8 and converted &gt;= 7:
  6. print(&quot;Breakfast time!&quot;)
  7. elif converted &lt;= 13 and converted &gt;= 12:
  8. print(&quot;Lunch time!&quot;)
  9. elif converted &lt;= 19 and converted &gt;= 18:
  10. print(&quot;Dinner time!&quot;)
  11. else:
  12. print(&quot;Bed time..&quot;)
  13. def convert(time):
  14. # splitting number
  15. splitted = time.split(&quot;:&quot;)
  16. # finding decimal value of time
  17. converted = int(splitted[0]) + (int(splitted[1])/60)
  18. # returning to main program
  19. return converted
  20. if __name__ == &quot;__main__&quot;:
  21. main()

when i run it in the mac terminal, it shows this error:

  1. What time is it? 7:30
  2. Traceback (most recent call last):
  3. File &quot;mealtime.py&quot;, line 30, in &lt;module&gt;
  4. main()
  5. File &quot;mealtime.py&quot;, line 2, in main
  6. user_time = str(input(&quot;What time is it? &quot;))
  7. File &quot;&lt;string&gt;&quot;, line 1
  8. 7:30
  9. ^
  10. SyntaxError: invalid syntax

is it just a minor issue and can anyone share a solution to this please

答案1

得分: 1

你的终端中的Python版本与VSCode中的不同。

如果你运行python mealtime.py,请检查使用python -V

为确保你的代码在Python 3中运行,请使用python3 mealtime.py

英文:

Your Python version in the terminal is different than in vscode.

If you run python mealtime.py, Check with python -V.

To ensure your code runs with python3, use python3 mealtime.py.

huangapple
  • 本文由 发表于 2023年7月12日 22:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定