Json 在python中调用类中的实例
我坐在一个研究项目中,试图为我们的AGV:s(自主引导Viechle)之一构建一个类。 我不会向全班展示,但我需要一些指针,以便我能为我的学生编写易于使用的程序 因此,我的代码如下所示(创建了标题): 在我第一次尝试使用该类时,我将其命名为:Json 在python中调用类中的实例,json,python-3.x,python-requests,python-class,Json,Python 3.x,Python Requests,Python Class,我坐在一个研究项目中,试图为我们的AGV:s(自主引导Viechle)之一构建一个类。 我不会向全班展示,但我需要一些指针,以便我能为我的学生编写易于使用的程序 因此,我的代码如下所示(创建了标题): 在我第一次尝试使用该类时,我将其命名为: mir.getHw() 得到了我要求的书面答复 然后我添加了用户输入,在本例中是一个ip,以便获取或发布到某个AGV(我们有2个): 这也起了作用 现在,针对我的问题,我尝试添加以下代码: while True: # Create an ins
mir.getHw()
得到了我要求的书面答复
然后我添加了用户输入,在本例中是一个ip,以便获取或发布到某个AGV(我们有2个):
这也起了作用
现在,针对我的问题,我尝试添加以下代码:
while True:
# Create an instance of the MiR class, pass the headers an ip
try:
userinput=input("""Which function do you want to use? \n\n
1) HardWare Info. \n\n
type q to quit \n\n
Please enter the corresponding number and hit enter >>>>> """)
if userinput == 1:
mir = mir(headers,ip)
mir.getHw()
elif userinput== "q":
break
except ValueError:
print("incorrect input")
continue
但是这里我没有从函数中得到打印输出,我如何解决这个问题?
我还有8个类似getHw的函数,我想在以后添加
//David在python3中,函数总是返回一个str
对象,因此永远不会产生您期望的1
值。您必须尝试将userinput
变量转换为int
,或者,更简单地说,将其与普通字符串进行比较,如下所示:
如果userinput='1':
...
非常感谢,我犯了这么愚蠢的错误!
#user input
while True:
try:
user_ip = input("Enter ip adress of MiR: ")
ip = ipaddress.ip_address(user_ip)
print(f'{ip} is correct. Version: IPv{ip.version}')
#convert back to string and insert into adress line for agv
ip=str(ip)
ip=insertChar(MiRTemplate,7,ip)
mir = mir(headers,ip)
mir.getHw
break
except ValueError:
print('ip Adress is invalid')
continue
while True:
# Create an instance of the MiR class, pass the headers an ip
try:
userinput=input("""Which function do you want to use? \n\n
1) HardWare Info. \n\n
type q to quit \n\n
Please enter the corresponding number and hit enter >>>>> """)
if userinput == 1:
mir = mir(headers,ip)
mir.getHw()
elif userinput== "q":
break
except ValueError:
print("incorrect input")
continue