python - 如何创建一个接受变量列表的 if 语句条件?
问题描述
如何在 if 语句中创建一个接受变量列表并打印出可被 5 整除的数字的条件?
firstNumber = input("Write the First Number: ")
secondNumber = input("Write the Second Number: ")
thirdNumber = input("Write the the Third Number: ")
fourthNumber = input("Write the the Fourth Number: ")
list1 = [firstNumber, secondNumber, thirdNumber, fourthNumber]
def divisibleByFive():
for x in list1:
if x%5 == 0:
print(x)
divisibleByFive()
解决方案
在函数中获取它作为参数并在调用时传递:
firstNumber = input("Write the First Number: ")
secondNumber = input("Write the Second Number: ")
thirdNumber = input("Write the the Third Number: ")
fourthNumber = input("Write the the Fourth Number: ")
list1 = [firstNumber, secondNumber, thirdNumber, fourthNumber]
def divisibleByFive(l1):
for x in l1:
if int(x)%5 == 0:
print(int(x))
divisibleByFive(list1)
不喜欢路过?使用全局变量,但这太过分了:-(
firstNumber = input("Write the First Number: ")
secondNumber = input("Write the Second Number: ")
thirdNumber = input("Write the the Third Number: ")
fourthNumber = input("Write the the Fourth Number: ")
list1 = [firstNumber, secondNumber, thirdNumber, fourthNumber]
def divisibleByFive():
global list1
for x in list1:
if int(x)%5 == 0:
print(int(x))
divisibleByFive()
推荐阅读
- regex - 使用 REGEX 查找包含特定字符串的所有 XML 文件
- javascript - 为什么我总是得到 6 而不是 21?
- c++ - 如何从指向变量的指针中永久赋值
- android - 搜索时如何隐藏 SeekBar 的刻度线?
- python - 在没有 Internet 访问权限的情况下安装 Python 模块
- python - OperationalError,没有这样的列:hospi_treatmentgiven.patient_id
- python - 这些行在计算 Mandelbrot 集时做了什么?
- python - 如何实现最大体积的分水岭分割
- jquery - rGraph y标签在数据为1时生成错误
- php - 更新功能以识别链接