用Python制作一个简单的计算器,但不知道如何不做其他事情
我想做一个程序,简单地加上或减去2个数字。我的代码如下:用Python制作一个简单的计算器,但不知道如何不做其他事情,python,python-3.x,Python,Python 3.x,我想做一个程序,简单地加上或减去2个数字。我的代码如下: x = int(input("Enter a number: ")) y = int(input("Enter a number: ")) print("Would you like to add or subtract?") txt = input("Type 'a' for add or 's' for subtract") if txt == "
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or "A":
x + y == z
if txt == "s" or "S":
x - y == z
else:
return
else:
return
print (z)
我知道返回值不正确,但不确定该如何计算。你需要做
z=x+y
和z=x-y
而不是x+y==z
和x-y==z
。Double等于是一个比较运算符
下面的代码应该可以工作:
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
z = 0
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or "A":
z = x + y
elif txt == "s" or "S":
z = x - y
//you can use return if it's a function
return z;
print (z)
您需要执行
z=x+y
和z=x-y
而不是x+y==z
和x-y==z
。Double等于是一个比较运算符
下面的代码应该可以工作:
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
z = 0
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or "A":
z = x + y
elif txt == "s" or "S":
z = x - y
//you can use return if it's a function
return z;
print (z)
首先,当你写作时:
x + y == z
您并没有定义一个新的z变量,只是使逻辑运算“x+y等于z吗?”其中z甚至没有定义。如果要将z定义为x和y的和或差,应使用:
z=x+y
或z=x-y
另外,当你想制作一个类似于“如果条件等于某物,如果条件等于其他物”的结构时,你可以使用if、else和elif(两者同时是else和if),但是你必须确保它们具有相同的缩进
以下代码应完成此工作:(已编辑)
编辑:您没有定义函数,因此不需要使用“return”。返回用于给出函数的结果,例如:
def sum(x, y):
z = x + y
return(z)
编辑#2:感谢您让我注意到
txt='a'或'a'
总是正确的,我现在已经修复了代码。首先,当您编写:
x + y == z
您并没有定义一个新的z变量,只是使逻辑运算“x+y等于z吗?”其中z甚至没有定义。如果要将z定义为x和y的和或差,应使用:
z=x+y
或z=x-y
另外,当你想制作一个类似于“如果条件等于某物,如果条件等于其他物”的结构时,你可以使用if、else和elif(两者同时是else和if),但是你必须确保它们具有相同的缩进
以下代码应完成此工作:(已编辑)
编辑:您没有定义函数,因此不需要使用“return”。返回用于给出函数的结果,例如:
def sum(x, y):
z = x + y
return(z)
编辑#2:谢谢你让我注意到
txt='a'或'a'
总是正确的,我现在已经修复了代码。首先:x+y==z
是对平等性的测试。它检查x+y
和z
是否相等。要将x+y
的结果分配给z
,需要执行z=x+y
对于此类问题,绘制流程图或至少写出代码中要实现的步骤确实很有帮助,尤其是在开始编程时。这里有一个例子。注意我的编号列表的缩进。这类似于您希望在Python代码中看到的缩进
返回一个字符串,因此转换为整数input()
获取字符串input()
- 做加法
- 减法
- 什么也不做?显示错误
x = int(input("Enter a number: ")) # 1.
y = int(input("Enter a number: ")) # 1.
print("Would you like to add or subtract?") # 2.
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or txt == "A": # 3.
z = x + y
elif txt == "s" or txt == "S": # 4.
z = x - y
else: # 5.
z = 0
print("Invalid choice!")
print (z) # 6.
您的代码在此处出错:
- 做加法
3.
中确定它是“a”或“a”)
- 减法
注1:在人类语言中,我们说 检查
txt
是否为“a”或“a”
但是Python在if
条件下使用布尔值。如果我们做了如果txt==“a”或“a”
,这将计算为如果[[(txt==“a”)为真]或[“a”为真]
。因为非空字符串在Python中是真实的,[“A”为true]
总是正确的,所以[(txt==“A”)为true]或[“A”为true]
总是为true
,所以我们总是进入if
语句。不是我们想要的!
我们必须对每一个单独进行比较,如下所示:
if txt == "a" or txt == "A":
或者,我们可以将txt
转换为小写或大写,并进行一次检查
if txt.lower() == "a":
或者
或者,我们可以检查txt
是否是列表中的元素之一
if txt in ["a", "A"]:
第一:
x+y==z
是对平等性的测试。它检查x+y
和z
是否相等。要将x+y
的结果分配给z
,需要执行z=x+y
对于此类问题,绘制流程图或至少写出代码中要实现的步骤确实很有帮助,尤其是在开始编程时。这里有一个例子。注意我的编号列表的缩进。这类似于您希望在Python代码中看到的缩进
返回一个字符串,因此转换为整数input()
获取字符串input()
- 做加法
- 减法
- 什么也不做?显示错误
x = int(input("Enter a number: ")) # 1.
y = int(input("Enter a number: ")) # 1.
print("Would you like to add or subtract?") # 2.
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or txt == "A": # 3.
z = x + y
elif txt == "s" or txt == "S": # 4.
z = x - y
else: # 5.
z = 0
print("Invalid choice!")
print (z) # 6.
您的代码在此处出错:
- 做加法