[英]I am struggling in data type of a single character in python. If it it is a string then comparison should be by string function
I am looking to add two numbers by accepting the response like this 12 + 56 Then I want to print 68 as output at the bottom of 56..it's showing some problems我希望通过接受这样的响应来添加两个数字 12 + 56 然后我想在 56 的底部打印 68 作为 output ..它显示了一些问题
def sum(a, b):
return (a + b)
def sub(a, b):
return (a - b)
def mul(a, b):
return (a*b)
def div(a, b):
return (a / b)
x - int(input(""))
z = str(input(""))
y = int(input(""))
if (z == '+'):
print(sum(x, y))
elif (z == '-'):
print(sub(x, y))
elif (z == '*'):
print(mul(x, y))
else:
print(div(x, y))
Um I have read the comment and try to summarise it here:)?嗯,我已经阅读了评论并尝试在这里总结它:)?
x = int(input(""))
use =
instead of -
to declare a variable and I think this is a typo? x = int(input(""))
使用=
而不是-
来声明一个变量,我认为这是一个错字?
s is not defined
to sum to number we can directly use the sum
function, in my opinion, no need to create a function for it: eg s is not defined
为总和,我们可以直接使用sum
function,在我看来,不需要为其创建 function:例如
x = input('')
y = input('')
print(sum([int(x), int(y)]))
full code ( changed ):完整代码(已更改):
def sum(a, b):
return (a + b)
def sub(a, b):
return (a - b)
def mul(a, b):
return (a*b)
def div(a, b):
return (a / b)
x = int(input("number : ")) # x - int(input("")) --> x = int(input("")) # typo i think
z = str(input("operator : "))
y = int(input("number : "))
'''
I change the x y z here : how to handle empty input
one of the way is using the if else statement
if x y z is empty --> nothing is done :
num1 = input("number : ") # and I prefer meaningful variable names : )
operator = input("operator : ")
num2 = input("number : ")
if num1.strip() == '' or num2.strip() == '':
print('error message')
'''
'''
or you can also make a function to recur it :
def askInput():
num1 = input("number : ")
operator = input("operator : ")
num2 = input("number : ")
if num1.strip() == '' or num2.strip() == '':
print("error")
askInput()
return num1, operator, num2
x, y, z = askInput()
print(x, y, z) # just to test if it works , you can delete it in you code : )
... # add sub mul div function here
'''
if (z == '+'):
print(sum(x, y)) # s is not defined --> i think it should be the function sum() right ?
# but sum is already a python built-in stuff ( idk what it call sry ) so not need to
# write a function for it i think
elif (z == '-'):
print(sub(x, y))
elif (z == '*'): # <-- i think it would be clear that z == '*' ?
print(mul(x, y))
else:
print(div(x, y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.