跳过整数进行实验室活动

问题描述 投票:0回答:4
  1. 定义一个名为skip_integers的函数,其参数数量可变。
  2. 使用 for 循环迭代参数。
  3. 使用检查来查看传递的值是否是整数类型。如果是,使用 continue 语句忽略它。
  4. 打印参数。
def skip_integers(*args):
     for i in args:
         if i == (function)
         continue

skip_integers(3,5.2, "value", 6.0)

我根本不明白这个问题有人可以帮助我吗:(

python function if-statement
4个回答
0
投票

问题不在于您所显示的代码,所以问题一定在于 if 语句。不知道你的函数在做什么,我创建了自己的函数。

   def is_integer(num): 
        if type(num) == int:
            return True 
        else: 
            return False

您还可以通过将我的函数中的 if 语句添加到您的代码中来简化此操作,如下所示。

    for i in args:
        if type(i) == int:
             continue

0
投票
user_string = input()    # prompt the user to enter the string    
result = user_string.isdigit()    # check if the string is an integer string

if result:    # if result is true then print yes
    print("yes")
else:    # else if result is false then print no
   print("no")

0
投票

这就是我想要获得准确输出的方式。

defskip_integers (*num): 对于数字中的 i: 如果类型(i)== int: 继续 打印(一)

skip_integers(5.2,“值”,6.0)


-1
投票

这有效:

def skip_integers(*args):
    args = list(args)
    for i in args:
        if type(i) == int:
            args.remove(i)
    return args
© www.soinside.com 2019 - 2024. All rights reserved.