英文:
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("What time is it? "))
# converting user_time to float
converted = convert(user_time)
if converted <= 8 and converted >= 7:
print("Breakfast time!")
elif converted <= 13 and converted >= 12:
print("Lunch time!")
elif converted <= 19 and converted >= 18:
print("Dinner time!")
else:
print("Bed time..")
def convert(time):
# splitting number
splitted = time.split(":")
# finding decimal value of time
converted = int(splitted[0]) + (int(splitted[1])/60)
# returning to main program
return converted
if __name__ == "__main__":
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 "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
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论