Python 3.x 我不知道';我不明白为什么这个代码赢了';我跑不了?
这是我在这里的第一个问题,所以,如果我犯了任何错误,很抱歉。 我正在尝试使用类在Python3上制作一个简单的计算器。然而,我不明白为什么它不会运行,它看起来很好。这是:Python 3.x 我不知道';我不明白为什么这个代码赢了';我跑不了?,python-3.x,Python 3.x,这是我在这里的第一个问题,所以,如果我犯了任何错误,很抱歉。 我正在尝试使用类在Python3上制作一个简单的计算器。然而,我不明白为什么它不会运行,它看起来很好。这是: class Math: def __init__(self,num1,num2): self.num1 = num1 self.num2 = num2 def add (self): print("Result: " + str(self.num1 + self
class Math:
def __init__(self,num1,num2):
self.num1 = num1
self.num2 = num2
def add (self):
print("Result: " + str(self.num1 + self.num2))
def subs (self):
print("Result: " + str(self.num1 - self.num2))
def mp (self):
print("Result: " + str(self.num1 * self.num2))
def div (self):
print("Result: " + str(self.num1 / self.num2))
operation = print((input("""Operations?:
1: Add
2: Substract
3: Multiply
4: Divide
""")))
num1 = print(input("Enter first number: "))
num2 = print(input("Enter second number: "))
maths = Math(num1,num2)
if operation == "1":
maths.add()
elif operation == "2":
maths.subs()
elif operation == "3":
maths.mp()
elif operation == "4":
maths.div()
else:
print("Undefined key")
在字符串上使用print()
时(例如input()
给出的字符串),它会打印出字符串并返回None
。您在下面设置的每个值都是None
。您不希望打印这些值,而是希望使用int()
函数将它们转换为整数,而不是打印
operation = print((input("""Operations?:
1: Add
2: Substract
3: Multiply
4: Divide
""")))
num1 = print(input("Enter first number: "))
num2 = print(input("Enter second number: "))
maths = Math(num1,num2)
这应该成为
operation = int((input("""Operations?:
1: Add
2: Substract
3: Multiply
4: Divide
""")))
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
maths = Math(num1,num2)
当您尝试运行它时会发生什么情况?是否有错误消息,或者只是没有输出,或者输出错误,或者它是否会永久暂停而不执行任何操作?
operation=print(…)
将None
分配给operation
。应该是operation=input(…)
@kaya3它询问操作,第一个和第二个数字,但随后它直接执行else语句。因此,您的程序确实运行了,但运行时它做了错误的事情。请编辑您的问题,具体说明它目前的作用,以及您希望它做什么。谢谢大家。我知道我错过了一些简单的东西。是的,现在我明白了。它现在运行。