Python 如何创建接受变量列表的if语句条件?
如何在if语句中创建一个接受变量列表并打印出可被5整除的数字的条件`Python 如何创建接受变量列表的if语句条件?,python,python-3.x,list,if-statement,user-input,Python,Python 3.x,List,If Statement,User Input,如何在if语句中创建一个接受变量列表并打印出可被5整除的数字的条件` 将其作为函数中的参数获取,并在调用时传递: firstNumber = input("Write the First Number: ") secondNumber = input("Write the Second Number: ") thirdNumber = input("Write the the Third Number: ") fourthNumber
将其作为函数中的参数获取,并在调用时传递:
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)
你不喜欢传球吗?使用全局变量,但这太过分了:-(
如果您计划修改列表,您只需要
global
,此处不需要。代码的主要问题是输入数据是字符串,需要在使用%
运算符之前转换为int
。将输入字符串转换为整数:firstNumber=int(输入(“写入第一个数字”)
,等等。
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()