【发布时间】:2020-06-07 00:47:10
【问题描述】:
我在编程的第一个月就遇到了我正在开发的应用程序的问题。我确信对于有经验的人来说这相当简单,但我不知道如何在没有语法错误的情况下修复它。这是我的代码(输出错误见下文)
def what_to_find():
find = (input(": "))
if find == int(1):
find_slope()
elif find == int(2):
find_elevation1()
elif find == int(3):
find_elevation2()
else:
print("Choose a number corresponding to what you want to find")
what_to_find()
所以输入功能有效,但无论我输入什么数字(1、2 或 3),else 命令下的“打印”总是会打印。例如,输出如下:
你想找到什么? 1 = 坡度,2 = 高海拔,3 = 低海拔 : 1 选择与您要查找的内容相对应的数字 插入更高的海拔:
因此,在此之后我有更多代码创建更高海拔的提示,但我只想知道如何确保它在运行后不打印 else 语句。我还在为我的 IDE 使用 Visual Studio Code。
来自非常缺乏经验的编码人员,在此先感谢您的帮助!
更新:在修改和使用其他人的输入后,这就是我所拥有的:
def what_to_find():
find = int(input(": "))
if find == 1:
find_slope()
elif find == 2:
find_elevation1()
elif find == 3:
find_elevation2()
else:
print("Choose a number corresponding to what you want to find")
what_to_find()
这一切都说得通,把它变成输出(在我插入 if 语句的相应数字之一之后):
What are you trying to find?
1 = Slope, 2 = Higher Elevation, 3 = Lower Elevation
: 1
Traceback (most recent call last):
File "gcalc.py", line 24, in <module>
what_to_find()
File "gcalc.py", line 15, in what_to_find
find_slope()
NameError: name 'find_slope' is not defined
不确定这是如何发生的,或者为什么更改开头的“find”会产生这个输出。请帮帮我!谢谢
【问题讨论】:
-
条件不成立。想想
input返回什么类型,1是什么类型。 -
您输入了错误的值。试试
int(find) == 1 -
type(find)是str。您将str与int进行比较。str和int不相等。 -
谢谢你们!我会试一试,让你知道
标签: python if-statement printing output