我是Python的新手。我试着制作一个基本的计算器,但我真的找不到问题。它返回0退出代码,但没有任何内容出现,没有任何输入。任何有关这方面的帮助将非常感激。谢谢。
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def div(num1, num2):
return num1/num2
def multi(num1,num2):
return num1*num2
def main():
operation = input("What do you want to do?(+, -, *, or /):")
if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
print("Your input is invalid. Please enter a valid input.")
else:
num1 = float(input("Enter value for num1: "))
num2 = float(input("Enter value for num2: "))
if (operation == "+"):
print(add(num1, num2))
elif (operation == "-"):
print(subtract(num1, num2))
elif (operation == "*"):
print(multi(num1,num2))
elif (operation == "/"):
print(div(num1,num2))
main()
答案 0 :(得分:1)
我想你错过了:
if __name__ == "__main__":
main()
您对main()
内部main
的来电本身无法执行,这可能是您无法获得任何意见的原因。
除此之外,您的代码应按预期工作(请确保您不要除以零;)。
编辑:为了让我的回答更加明显,你应该做到:
def main():
operation = input("What do you want to do?(+, -, *, or /):")
if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
print("Your input is invalid. Please enter a valid input.")
else:
num1 = float(input("Enter value for num1: "))
num2 = float(input("Enter value for num2: "))
if (operation == "+"):
print(add(num1, num2))
elif (operation == "-"):
print(subtract(num1, num2))
elif (operation == "*"):
print(multi(num1,num2))
elif (operation == "/"):
print(div(num1,num2))
if __name__ == "__main__":
main()
答案 1 :(得分:1)
根据上面的代码,您实际上永远不会运行main()
。现在,您已经说过main
的定义是提示用户,检查输入是否正确,然后进行数学运算。最后的main()
导致程序在执行完所有操作后重复(不确定是否需要循环)。
如果你不想要循环,只想运行计算器一次,只需删除最后一个main()
的缩进,因为现在缩进意味着它在def main()
内。只需将其向左移动到与def main():
相同的缩进级别,您的程序就可以正常运行。
答案 2 :(得分:1)
func loadBy(_ req: Request) throws -> Future<[News]> {
let parentId = try req.parameters.next(String.self)
let parent = try self.findParentBy(req, parentId: parentId)
var childNews: [News] = []
return parent.flatMap(to: [News].self) { parentNews in
return parentNews!.childIds.map { (key) in
return News.query(on: req).filter(\News.newsId == key).first().map(to: Void.self) { child in
if let child = child {
childNews.append(child)
}
}
}.flatten(on: req).map {
return childNews
}
}
}
func loadBy(_ req: Request) throws -> Future<[News]> {
let parentId = try req.parameters.next(String.self)
let parent = try self.findParentBy(req, parentId: parentId)
return parent.flatMap(to: [News].self) { parentNews in
return News.query(on: req).filter(\News.newsId ~~ parentNews!.childIds).all()
}
}
答案 3 :(得分:0)
基本计算器:
方法1:
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number (Should be in numeric form): "))
num2 = float(input("Enter second number (Should be in numeric form): "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
方法2:
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number (Should be in numeric form): "))
num2 = float(input("Enter second number (Should be in numeric form): "))
if choice == '1':
print(num1,"+",num2,"=", num1+num2)
elif choice == '2':
print(num1,"-",num2,"=", num1-num2)
elif choice == '3':
print(num1,"*",num2,"=", num1*num2)
elif choice == '4':
print(num1,"/",num2,"=", num1/num2)
else:
print("Invalid input")
学习愉快...:)
答案 4 :(得分:0)
while True:
print("PRINT 'E' for exit")
given_number=input("Give the operation :")
given_number_in_string=str(given_number)
position_of_multiplication=(given_number_in_string.find("*"))
position_of_addition=(given_number_in_string.find("+"))
position_of_substraction=(given_number_in_string.find("-"))
position_of_division=(given_number_in_string.find("/"))
if position_of_multiplication >= 0:
position_of_multiplication=(given_number_in_string.find("*"))
first_no=float(given_number_in_string[0:position_of_multiplication])
second_no=float(given_number_in_string[position_of_multiplication+1:16])
print(f"answer is {first_no*second_no}")
a=input("")
elif position_of_addition >= 0:
position_of_addition=(given_number_in_string.find("+"))
first_no=float(given_number_in_string[0:position_of_addition])
second_no=float(given_number_in_string[position_of_addition+1:16])
print(f"answer is {first_no+second_no}")
s=input("")
elif position_of_substraction >= 0:
position_of_substraction=(given_number_in_string.find("-"))
first_no=float(given_number_in_string[0:position_of_substraction])
second_no=float(given_number_in_string[position_of_substraction+1:16])
print(f"answer is {first_no-second_no}")
s=input("")
elif position_of_division >= 0:
position_of_division=(given_number_in_string.find("/"))
first_no=float(given_number_in_string[0:position_of_division])
second_no=float(given_number_in_string[position_of_division+1:16])
print(f"answer is {first_no/second_no}")
s=input("")
if given_number_in_string == "E":
break