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

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

mac terminal error but not vscode's terminal

问题

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

程序如下:

def main():
    user_time = str(input("What time is it? "))

    # 将user_time转换为浮点数
    converted = convert(user_time)

    if converted <= 8 and converted >= 7:
        print("早餐时间!")
    elif converted <= 13 and converted >= 12:
        print("午餐时间!")
    elif converted <= 19 and converted >= 18:
        print("晚餐时间!")
    else:
        print("睡觉时间..")

def convert(time):
    # 分割时间
    splitted = time.split(":")

    # 找到时间的小数值
    converted = int(splitted[0]) + (int(splitted[1])/60)

    # 返回给主程序
    return converted

if __name__ == "__main__":
    main()

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

What time is it? 7:30
Traceback (most recent call last):
  File "mealtime.py", line 30, in <module>
    main()
  File "mealtime.py", line 2, in main
    user_time = str(input("What time is it? "))
  File "<string>", line 1
    7:30
     ^
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:

def main():
    user_time = str(input(&quot;What time is it? &quot;))

    # converting user_time to float
    converted = convert(user_time)

    if converted &lt;= 8 and converted &gt;= 7:
        print(&quot;Breakfast time!&quot;)
    elif converted &lt;= 13 and converted &gt;= 12:
        print(&quot;Lunch time!&quot;) 
    elif converted &lt;= 19 and converted &gt;= 18:
        print(&quot;Dinner time!&quot;)
    else:
        print(&quot;Bed time..&quot;)
            


def convert(time):
    # splitting number
    splitted = time.split(&quot;:&quot;)

    # finding decimal value of time
    converted = int(splitted[0]) + (int(splitted[1])/60)

    # returning to main program
    return converted


if __name__ == &quot;__main__&quot;:
    main()

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

What time is it? 7:30
Traceback (most recent call last):
  File &quot;mealtime.py&quot;, line 30, in &lt;module&gt;
    main()
  File &quot;mealtime.py&quot;, line 2, in main
    user_time = str(input(&quot;What time is it? &quot;))
  File &quot;&lt;string&gt;&quot;, line 1
    7:30
     ^
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:

确定