Python 在if语句中调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20526905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:40:17  来源:igfitidea点击:

Calling function inside if statement

pythonfunctionif-statement

提问by Joe Doe

im trying to call function inside if statement but it does not work. This is one of my first attempts in using Python. What am I doing wrong?

我试图在 if 语句中调用函数,但它不起作用。这是我使用 Python 的第一次尝试。我究竟做错了什么?

#!/usr/bin/python


menu = raw_input ("Hello, please choose form following options (1,2,3) and press enter:\n"
    "Option 1\n"
    "Option 2\n"
    "Option 3\n")

if menu == str("1"):
    savinginfile = raw_input ("Please, state your name: ")
    option1()
elif menu == str("2"):
    print ("Option 2")
elif menu == str("3"):
    print ("Option 3")

def option1():
    test = open ("test.txt", "rw")
    test.write(savinginfile)
    print ("Option 1 used")
    test.close()

回答by Adam Smith

You need to define your function before you try to call it. Just put def option1(): #and all that code below itabove your if statements.

在尝试调用它之前,您需要定义您的函数。只需放在def option1(): #and all that code below it您的 if 语句上方即可。

It's also bad practice to throw around too many global variables. You shouldn't use savinginfilethe way you are -- instead, pass it to the function as a parameter and let the function operate in its own scope. You'll need to pass the function the name of the file to use before it's able to use savinginfile. Try instead:

抛出太多全局变量也是不好的做法。你不应该使用savinginfile你现在的方式——相反,将它作为参数传递给函数,让函数在它自己的范围内运行。您需要将要使用的文件名传递给函数,然后才能使用savinginfile. 试试吧:

def option1(whattosaveinfile):
  test = open("test.txt","a+") #probably better to use a with statement -- I'll comment below.
  test.write(whattosaveinfile) #note that you use the parameter name, not the var you pass to it
  print("Option 1 used")
  test.close()

#that with statement works better for file-like objects because it automatically
#catches and handles any errors that occur, leaving you with a closed object.
#it's also a little prettier :) Use it like this:
#
# with open("test.txt","a+") as f:
#   f.write(whattosaveinfile)
# print("Option 1 used")
#
#note that you didn't have to call f.close(), because the with block does that for you
#if you'd like to know more, look up the docs for contextlib

if menu == "1": #no reason to turn this to a string -- you've already defined it by such by enclosing it in quotes
  savinginfile = raw_input("Please state your name: ")
  option1(savinginfile) #putting the var in the parens will pass it to the function as a parameter.

elif menu == "2": #etc
#etc
#etc

回答by qwwqwwq

Would recommend that you pass savinginfileas a parameter:

建议您savinginfile作为参数传递:

def option1(savinginfile):
    test = open ("test.txt", "rw")
    test.write(savinginfile)
    print ("Option 1 used")
    test.close()

You need to define option1before calling. Python interprets from top to bottom.

您需要option1在调用之前定义。Python 从上到下解释。