action = input("Enter the statement [add] or E to exit the program: ")
if action == "add":
x1 = int(input("enter the first number: "))
x2 = int(input("enter the second number: "))
print("Answer: Add {} and {} = {}". format(x1,x2,x1+x2))
elif action == "E" or action == "e":
# exit action
exit("Goodbye")
else:
# unexpected action
exit("Error: invaild action {}".format(action))
您需要使用.split()来分隔字符串中包含的用户输入。 (https://docs.python.org/3/library/stdtypes.html#str.split)
但是您应该检查用户是否使用了“add”和“and”using if,以确保用户语句符合您的要求。你知道吗
相关问题 更多 >
编程相关推荐