我正在尝试设置'help'的全局变量来显示我在代码中的函数。目标是用户在“帮助”一词中输入的任何时候我都可以显示我创建的表格。该函数称为table(),我想显示它。 我的代码如下:
def table():
header=(" Operation Command-line Format")
underline=("_________ ___________________")
meat=("Union set1&set2 \n Intersection set1|set2 \n Difference set1-set2 \n Symmetric difference set1 ^ set2 \n Cartesian product set1 X set2 \n Subset set1<=set2 \n Proper subset set1<set2 \n Super set set1=>set2 \n Proper super set set1>set2 \n Cardinality card set1 \n Membership-in xE set1 where x is single element\n Membership-not-in x!Eset1 where x is single element\n Power set power set1\n Display this table help")
print(header, "\n",underline,"\n",meat)
def intro():
firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your own")
if input is 'help':
table()
if firstChoice== '1':
fileChoice=input("Please enter the fle you would like to open")
with open(fileChoice, 'r+') as f:
z=f.readlines()
length=len(z)
try:
if z != len(z):
fileSet1=z[length-length]
if length-length != " ":
fileSet1=fileSet1.strip('\n')
fileSet1=str(fileSet1)
fileSet1.split(',')
fileSet1=set([fileSet1])
fileSet2=z[length-length+1]
if length-length+1 != " ":
fileSet2=fileSet2.strip('\n')
fileSet2=str(fileSet2)
fileSet2.split(',')
fileSet2=set([fileSet2])
fileSet3=z[length-length+2]
if length-length+2 != " ":
fileSet3=fileSet3.strip('\n')
fileSet3=str(fileSet3)
fileSet3.split(',')
fileSet3=set([fileSet3])
fileSet4=z[length-length+3]
if length-length +3 != " ":
fileSet4=fileSet4.strip('\n')
fileSet4=str(fileSet4)
fileSet4.split(',')
fileSet4=set([fileSet4])
print ("This is fileSet1 \n",fileSet1)
print ("This is fileSet2 \n",fileSet2)
print ("This is fileSet3 \n",fileSet3)
print ("This is fileSet4 \n",fileSet4)
except:
try:
print ("This is fileSet1 \n",fileSet1,"\nThis is fileSet2 \n",fileSet2,"\nThis is fileSet3 \n",fileSet3)
except:
print ("This is fileSet1 \n",fileSet1,"\nThis is fileSet2 \n",fileSet2)
f.close()
elif firstChoice== '2':
keep_going= 'yes'
userSet=input("Please enter the set you would like to use. " )
userSet1=userSet.split(',')
userset1=set(userSet1)
print("User set 1 ", userset1)
keep_going=input("would you like to go again? Please respond with yes or no")
while keep_going == 'yes':
userSet=input('what other set would you like to use? ')
userSet2=userSet.split(',')
userset2=set(userSet2)
print ("user set 2 ", userset2)
keep_going=input ("Would you like to use another set too? ")
if keep_going== 'yes':
userSet=input("Th other set you would like to use?")
userSet3=userSet.split(',')
userset3=set([userSet3])
print("user set 3 ", userset3)
keep_going=input("would you like to keep going? ")
if keep_going== 'yes':
userSet=input("Th other set you would like to use?")
userSet4=userSet.split(',')
userset4=set([userSet4])
print("user set 4 ", userset4)
keep_going==input("Another one?")
if keep_going == 'yes':
print ("i don't think we need another set")
keep_going='no'
firstSet=input("which set would you like to use for operations \nplease type in userset1,userset2, userset3, userset4 or \n fileSet1,fileSet2,fileSet3 or fileSet4 ")
if firstSet == "userset1":
set1=userset1
print ("this is set1",set1)
elif firstSet =="userset2":
set1=userset2
print ("this is set1",(set1))
elif firstSet =="userset3":
set1=userset3
print ("this is set1",(set1))
elif firstSet =="userset4":
set1=userset4
print ("this is set1",(set1))
elif firstSet =="fileSet1":
set1=fileSet1
print ("this is set1",(set1))
elif firstSet =="fileSet2":
set1=fileSet2
print ("this is set1",(set1))
elif firstSet =="fileSet3":
set1=fileSet3
print ("this is set1",(set1))
elif firstSet =="fileSet4":
set1=fileSet4
print ("this is set1",(set1))
else:
print ('your bad')
secondSet=input("What other set would you like to use for operations \n please type in userset1,userset2, userset3, userset4, setFile ")
if secondSet == "userset1":
set2=userset1
print ("this is set2",set2)
elif secondSet =="userset2":
set2=userset2
print ("this is set2",(set2))
elif secondSet =="userset3":
set2=userset3
print ("this is set2",(set2))
elif secondSet =="userset4":
set2=userset4
print ("this is set2",(set2))
elif secondSet =="fileSet1":
set2=fileSet1
print ("this is set1",(set1))
elif secondSet =="fileSet2":
set2=fileSet2
print ("this is set1",(set1))
elif secondSet =="fileSet3":
set2=fileSet3
print ("this is set1",(set1))
elif secondSet =="fileSet4":
set2=fileSet4
print ("this is set1",(set1))
else:
print ('your bad')
table()
commandinput=input("please select the operation you would like to do")
commandinput=commandinput.split()
if '&' in commandinput:
union=set1.union(set2)
print(union)
elif '|' in commandinput:
intersetction=set1.intersection(set2)
print(intersection)
elif '-' in commandinput:
difference=set1/difference(set2)
print(difference)
elif '^' in commandinput:
symmetric=set1.symmetric_difference(set2)
print(symmetric)
elif 'X' in commandinput:
print ("HIGH")
elif '<=' in commandinput:
subset=set2.issubset(set1)
print(subset)
elif '<' in commandinput:
print ("HIGH")
elif '=>' in commandinput:
subset=set2.issuperset(set1)
print(subset)
elif '>' in comandinput:
print ("HIGH")
elif 'card' in commandinput:
length=len(set1)
print(length)
elif 'E' in commandinput:
x=input("what would you like to search for")
if x in set1:
print ( "It is in here!!")
else:
print("It isn't in here")
elif '!E' in commandinput:
x=input("what would you like to search for ")
if x in set1:
print ("It is in here")
else:
print ("Not here!")
elif "power" in commandinput:
print ("HIGH")
elif 'help' in commandinput:
print(table())
intro()
答案 0 :(得分:5)
您应该使用==
,而不是is
。 is
测试字符串是否是内存中完全相同的对象,它们可能不是。
if firstChoice == 'help':
答案 1 :(得分:1)
我认为您正在检查错误的变量名称。
firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your own") if input is 'help': table()
应该是
firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your own") if firstChoice is 'help': table()