不知道如何使用 if 函数在 python 中调用 类
Don't know how to call classes in python using if function
所以我正在制作一个程序,它读取一个 .txt 文件和 运行s 不同的 classes;我做到了,所以它打开文件并将文本存储在决策字符串中,使用 if decision == "wincode" or "lostcode" 我希望它 运行 一个特定的 class 像:
class wonToss():
wininput = input("Do you want to choose: ")
def outChoice():
print("no error")
def inChoice():
print("No error")
if wininput == "Indoor":
inChoice()
elif wininput == "Outdoor":
outChoice()
我把“没有错误”放在测试看是否有效
class lostToss():
def botChoose():
print("Bot is choosing between Indoor and Outdoor.")
print(".")
time.sleep(0.25)
print("..")
time.sleep(0.25)
print("...")
time.sleep(0.25)
print("")
choices = ['Indoor', 'Outdoor']
botting = random.choice(choices)
print(f"And the bot chose: {botting}")
time.sleep(2)
botChoose()
这是第二个class。所以现在,我希望它检查代码和 class 具体 class.
这是验证码:
f=open("TossDecision.ch", "r")
if f.mode == 'r':
decision = f.read()
if decision == "jhe78r2dbgv67t2yhdb7vgr":
print("")
wonToss()
elif decision == "jhe78rnxshwdn7wgh89u3cn":
print("")
lostToss()
else:
print("Toss Decision changed! Re-run the PlayGame file.")
exit()
但问题是,
当我 运行 程序时它只显示:
Do you want to choose (Indoor) or (Outdoor)?: Indoor
Bot is choosing between Indoor and Outdoor.
.
..
...
机器人选择了:室内
它调用了两个 classes。目前它设置为损失。它仍然调用 wonToss()
谁能帮忙解决这个问题
抱歉,您使用的 class 完全错误。
请阅读Object-oriented编程。
一个class是一个对象的蓝图。
在大多数情况下,您实例化 class 的一个对象并使用它。像这样:
class Mything:
def InChoice(self):
print("No errror indoors")
def OutChoice(self):
print("No error outdoors")
def ask_choice(self):
wininput = input("Do you want to choose: ")
if wininput == "Indoor":
self.InChoice()
elif wininput == "Outdoor":
self.OutChoice()
然后在你的程序中你做:
choice_object=Mything()
choice_object.ask_choice()
所以我正在制作一个程序,它读取一个 .txt 文件和 运行s 不同的 classes;我做到了,所以它打开文件并将文本存储在决策字符串中,使用 if decision == "wincode" or "lostcode" 我希望它 运行 一个特定的 class 像:
class wonToss():
wininput = input("Do you want to choose: ")
def outChoice():
print("no error")
def inChoice():
print("No error")
if wininput == "Indoor":
inChoice()
elif wininput == "Outdoor":
outChoice()
我把“没有错误”放在测试看是否有效
class lostToss():
def botChoose():
print("Bot is choosing between Indoor and Outdoor.")
print(".")
time.sleep(0.25)
print("..")
time.sleep(0.25)
print("...")
time.sleep(0.25)
print("")
choices = ['Indoor', 'Outdoor']
botting = random.choice(choices)
print(f"And the bot chose: {botting}")
time.sleep(2)
botChoose()
这是第二个class。所以现在,我希望它检查代码和 class 具体 class.
这是验证码:
f=open("TossDecision.ch", "r")
if f.mode == 'r':
decision = f.read()
if decision == "jhe78r2dbgv67t2yhdb7vgr":
print("")
wonToss()
elif decision == "jhe78rnxshwdn7wgh89u3cn":
print("")
lostToss()
else:
print("Toss Decision changed! Re-run the PlayGame file.")
exit()
但问题是, 当我 运行 程序时它只显示:
Do you want to choose (Indoor) or (Outdoor)?: Indoor
Bot is choosing between Indoor and Outdoor.
.
..
...
机器人选择了:室内
它调用了两个 classes。目前它设置为损失。它仍然调用 wonToss()
谁能帮忙解决这个问题
抱歉,您使用的 class 完全错误。
请阅读Object-oriented编程。
一个class是一个对象的蓝图。
在大多数情况下,您实例化 class 的一个对象并使用它。像这样:
class Mything: def InChoice(self): print("No errror indoors") def OutChoice(self): print("No error outdoors") def ask_choice(self): wininput = input("Do you want to choose: ") if wininput == "Indoor": self.InChoice() elif wininput == "Outdoor": self.OutChoice()
然后在你的程序中你做:
choice_object=Mything()
choice_object.ask_choice()