当变量在外部时未调用函数 运行ning,但不会 运行 除非在变量内部时调用
Uncalled function running when variable is outside, but doesn't run unless called when variable is inside
如果您在 configselect() 内部引用 configtype,则该函数不会 运行 除非被调用,但是当在 configselect() 外部引用 configtype 时,input() 函数将 运行 而不是将返回值。
你们觉得这里发生了什么?
import os
configtype = int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? "))
def configselect():
if configtype == 1:
print("you chose 962")
elif configtype == 2:
print("you chose Audience")
elif configtype == 3:
print("you chose mAP")
else:
print("Try again dummy")
为了运行一个函数,你需要调用它的名字。
import os
configtype = int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? "))
def configselect():
if configtype == 1:
print("you chose 962")
elif configtype == 2:
print("you chose Audience")
elif configtype == 3:
print("you chose mAP")
else:
print("Try again dummy")
configselect()
以上会运行.
另外,一般来说,函数应该写成
def configselect(choice):
if choice == 1:
print("you chose 962")
elif choice == 2:
print("you chose Audience")
elif choice == 3:
print("you chose mAP")
else:
print("Try again dummy")
以便它更易于重复使用。
最后,为了让您的函数不总是打印“Try again dummy”,您应该将 configtype 转换为整数。输入总是 return 一个字符串。
如果您在 configselect() 内部引用 configtype,则该函数不会 运行 除非被调用,但是当在 configselect() 外部引用 configtype 时,input() 函数将 运行 而不是将返回值。
你们觉得这里发生了什么?
import os
configtype = int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? "))
def configselect():
if configtype == 1:
print("you chose 962")
elif configtype == 2:
print("you chose Audience")
elif configtype == 3:
print("you chose mAP")
else:
print("Try again dummy")
为了运行一个函数,你需要调用它的名字。
import os
configtype = int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? "))
def configselect():
if configtype == 1:
print("you chose 962")
elif configtype == 2:
print("you chose Audience")
elif configtype == 3:
print("you chose mAP")
else:
print("Try again dummy")
configselect()
以上会运行.
另外,一般来说,函数应该写成
def configselect(choice):
if choice == 1:
print("you chose 962")
elif choice == 2:
print("you chose Audience")
elif choice == 3:
print("you chose mAP")
else:
print("Try again dummy")
以便它更易于重复使用。
最后,为了让您的函数不总是打印“Try again dummy”,您应该将 configtype 转换为整数。输入总是 return 一个字符串。