美文网首页
Python快速入门 - 条件判断

Python快速入门 - 条件判断

作者: AaronYin | 来源:发表于2021-05-11 09:27 被阅读0次
x = input('请输入第一个数字(x): ')
y = input('请输入第二个数字(y): ')
operator = input('请输入一个运算符: ')

x = int(x)
y = int(y)
result = None

if operator == '+':
  result = x + y
elif operator == '-':
  result = x - y
elif operator == '*':
  result = x * y
elif operator == '/':
  result = x / y
elif operator == '%':
  result = x % y
elif operator == '**':
  result = x ** y

if result == None:
  print('未能识别您输入的内容')
else:
  print('%d %s %d = %d'%(x, operator, y, result))

测试:

请输入第一个数字(x): 5
请输入第二个数字(y): 3
请输入一个运算符: **
5 ** 3 = 125

请输入第一个数字(x): 5
请输入第二个数字(y): 3
请输入一个运算符: ***
未能识别您输入的内容
if None:
  print('...')

if "":
  print('...')

if 0:
  print('...')

if []:
  print('...')

if {}:
  print('...')

所以None、0、空字符串、空数组、空字典都相当于False。

相关文章

网友评论

      本文标题:Python快速入门 - 条件判断

      本文链接:https://www.haomeiwen.com/subject/ayaydltx.html