【问题标题】:How would I put an if statement inside a function?如何在函数中放置 if 语句?
【发布时间】:2015-08-20 08:51:21
【问题描述】:

我对 Python 非常陌生,在此之前我只使用带有标签和 goto 的极其简单的“编程”语言。我正在尝试使此代码在 Sikuli 中工作:

http://i.imgur.com/gbtdMZF.png

基本上我希望它循环if 语句,直到找到任何图像,如果找到其中一个,它会执行正确的函数并再次开始循环,直到它检测到新命令。

我一直在寻找教程和文档,但我真的迷路了。我认为我的大脑正忙于尝试从 goto/label 转换为有组织的编程语言。

如果有人能给我一个例子,我将不胜感激!

【问题讨论】:

  • 在 python 缩进问题中的代码中提供缩进 function 下一行应该缩进
  • 把你的代码放在你的问题中
  • @VigneshKalai Ughh 很抱歉,我不知道实际上需要格式化......我以前从未在编程语言中看到过,通常它仍然可以运行。我现在觉得自己像个白痴……好吧,谢谢!
  • 对于你以后的问题,你应该把你的代码直接放到问题中,而不是截图。
  • python初学者的大部分问题都在教程中回答:docs.python.org/3/tutorial

标签: python function if-statement syntax indentation


【解决方案1】:

在 Python 缩进问题上,您的代码应如下所示:

def function():
    if condition1:
        reaction1() 
    elif condition2:
        reaction2()
    else:
        deafult_reaction()

我建议阅读 Dive Into Python 中的 the chapter about indentation 以及 PEP 0008

【讨论】:

    【解决方案2】:
    x = input( "please enter the variable")
    print(" the Variable entered is " + x)
    myfloat = int(x)
    def compare (num):
         if num%2 == 0:
              print(" entered variable is even")
         else: 
              print("entered variable is odd")
         compare(myfloat)
    

    【讨论】:

    • 这段代码有一个无限循环,但我无法编辑它
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2021-01-13
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多