简体   繁体   English

我在 python 中单个字符的数据类型上苦苦挣扎。 如果它是一个字符串,那么比较应该是字符串 function

[英]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.

相关问题 我无法在 Python 中使用 for 循环使用字符串中特定字符的索引。它给出了索引错误 - I am not able to use index of a particular character in a string using a for loop in Python. It's giving an index error 如何在 python 中的字符串之间添加空格。 我正在从 ms access 数据库中获取数据 - how to add space between a string in python. I am fetching the data from ms access database Python. function 中的反向字符串 - Python. reverse string in a function 如何在 python 中为 int 和 string 类型调用 function。 我附上了我的代码片段 - How to call a function for both int and string type in python. I have attached the snippets of my code Python字符串比较功能() - Python String Comparison function() 我是 Pandas 和 Python 的新手。我正在为 & 和 groupby 的语法而苦苦挣扎 - I am new to Pandas and Python. I am struggling with syntax of & and groupby 使用 python 打印范围之间的奇数或偶数列表。 输出应与字符串在一行中 - Print the list of odd or even numbers between a range using python. Output should be in a single line along with the string 蟒蛇。 分割一部分以特定字符开头和以字符结尾的字符串 - Python. slice a part of the string that begins with a specific character and end with a character 蟒蛇。 字符串编码 - Python. The coding of a string Python。 如何每隔n个字符用空格分隔字符串? - Python. How to separate string with whitespace every nth character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM