Python 3.x Python代码在VS代码中既不能正常运行也不能显示错误
我正在使用Vs代码(Charm在我的PC上工作得不太顺畅)进行python开发,调试后,它没有显示错误或输出 我在堆栈溢出中搜索匹配的解决方案Python 3.x Python代码在VS代码中既不能正常运行也不能显示错误,python-3.x,visual-studio-code,vscode-settings,Python 3.x,Visual Studio Code,Vscode Settings,我正在使用Vs代码(Charm在我的PC上工作得不太顺畅)进行python开发,调试后,它没有显示错误或输出 我在堆栈溢出中搜索匹配的解决方案 weight = int(input("Enter weight: ")) unit = input("(K)kilograms or (P)pounds? ") if unit.upper == "P": (weight*=1.6) print("Weight in kilograms: " + weight) else if U
weight = int(input("Enter weight: "))
unit = input("(K)kilograms or (P)pounds? ")
if unit.upper == "P":
(weight*=1.6)
print("Weight in kilograms: " + weight)
else if Unit=="K":
(weight*=1.6)
print("Weight in pounds: " + weight)
else:
print("ERROR INPUT IS WRONG!")
我希望它接受输入并提供转换后的输出您的脚本:
- 遗漏了一个()
- 使用未知名称
单元
- 尝试添加字符串和数字:
print(“以磅为单位的重量:+Weight)
- 英镑计算错了吗
- 在不适用的情况下使用()
- 如果…,则使用
else:代码>
您只需在输入时直接使用
.upper()
:
# remove whitespaces, take 1st char only, make upper
unit = input("(K)kilograms or (P)pounds? ").strip()[0].upper()
更好的可能是:
weight = int(input("Enter weight: "))
while True:
# look until valid
unit = input("(K)kilograms or (P)pounds? ").strip()[0].upper()
if unit in "KP":
break
else:
print("ERROR INPUT IS WRONG! K or P")
if unit == "P":
weight /= 1.6 # fix here need divide
print("Weight in kilograms: ", weight) # fix here - you can not add str + int
else:
weight *= 1.6
print("Weight in pounds: ", weight)
您应该查看str.format:
print("Weight in pounds: {:.03f}".format(weight)) # 137.500
见f.e
print("Weight in pounds: {:.03f}".format(weight)) # 137.500