gpt4 book ai didi

python - 在 if 语句中调用函数

转载 作者:行者123 更新时间:2023-11-28 21:21:55 26 4
gpt4 key购买 nike

我试图在 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()

最佳答案

建议您将 savinginfile 作为参数传递:

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

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

关于python - 在 if 语句中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com