用于计算数字重复次数的简单程序

时间:2018-06-13 07:15:46

标签: python python-3.x function input args

我是学习python 3的初学者。我遇到了一个问题。请看三星内的代码:

s = input("Enter the value of N A B with white space in between ")
N, A, B=list(s.split())

def input_for_faces(*args):
    NumberOfA=0
    NumberOfB=0
    for x in args:
        if x == int(A):
            NumberOfA += 1
    for y in args:
        if y == int(B):
            NumberOfB += 1
    listAB=[NumberOfA, NumberOfB]
    return listAB

# ***
var=input("Enter the values on the faces of the cube seperated by commas ")
NA, NB=input_for_faces(var)
print(input_for_faces(var))
# ***

print("The probability of the chef winning is "+str((int(NA)/int(N)*(int(NB)/int(N))))

*args的这种输入方法没有给出正确的输出(它有效,但给出了错误的答案)。但是当我为args提供直接值时,程序运行正常。

直接输入是指:

s = input("Enter the value of N A B with white space in between ")
N, A, B=list(s.split())

def input_for_faces(*args):
    NumberOfA=0
    NumberOfB=0
    for x in args:
        if x == int(A):
            NumberOfA += 1
    for y in args:
        if y == int(B):
            NumberOfB += 1
    listAB=[NumberOfA, NumberOfB]
    return listAB

# ***
NA, NB=input_for_faces(1,1,1,1,1)
# ***

print("The probability of the chef winning is "+str((int(NA)/int(N))*(int(NB)/int(N))))

请告诉我我做错了什么。

2 个答案:

答案 0 :(得分:1)

1,在' if' 条件的本部分代码中,您将字符串与整数进行比较,以便条件变为false并且不计算 ' NumberOfA'

for x in args:
        if x == int(A):
            NumberOfA += 1
    for y in args:
        if y == int(B):
            NumberOfB += 1
    listAB=[NumberOfA, NumberOfB]

输出:

Enter the value of N A B with white space in between 1 1 1
Enter the values on the faces of the cube seperated by commas 1,1,1,1,1
[0, 0]
The probability of the chef winning is 0.0
>>> 

2,您将传递单个字符串作为 * args 的输入 所以要传递多个参数,你将输入转换为列表 lvar = var.split(','),然后在函数调用中使用 * lvar {NA,NB = input_for_faces(* lvar)},将从列表中逐个传递多个参数

s =输入("输入N A B的值,其间为空格")     N,A,B = list(s.split())

def input_for_faces(*args):
    NumberOfA=0
    NumberOfB=0
    for x in args:
        if x == A:
            NumberOfA += 1
    for y in args:
        if y == B:
            NumberOfB += 1
    listAB=[NumberOfA, NumberOfB]
    return listAB

# ***
var=input("Enter the values on the faces of the cube seperated by commas ")
lvar=var.split(',')
NA, NB=input_for_faces(*lvar)
print(input_for_faces(*lvar))
# ***

print("The probability of the chef winning is "+str((int(NA)/int(N)*(int(NB)/int(N)))))

输出:

Enter the value of N A B with white space in between 1 1 1
Enter the values on the faces of the cube seperated by commas 1,1,1,1,1
[5, 5]
The probability of the chef winning is 25.0
  
    
      

    
  

答案 1 :(得分:0)

如果您不想或不必使用* args,您可以这样做:

s = raw_input("Enter the value of N A B with white space in between ")
N, A, B=list(s.split())

def input_for_faces(inputList):
    NumberOfA=0
    NumberOfB=0
#You only need to loop through once for A and B, no need to do each separately
#You can still use the old one though if you want to
    for number in inputList:
        if number == int(A):
            NumberOfA += 1
        if number == int(B):
            NumberOfB += 1
    listAB =[NumberOfA, NumberOfB]
    return listAB

var =raw_input("Enter the values on the faces of the cube seperated by commas: ").split(",")
inputList = [int(i) for i in var]
NA,NB = input_for_faces(inputList)
print(input_for_faces(inputList))
print("The probability of the chef winning is "+str((int(NA)/int(N))*(int(NB)/int(N))))

使用* args:

s = raw_input("Enter the value of N A B with white space in between ")
N, A, B=list(s.split())

def input_for_faces(*args):
    NumberOfA=0
    NumberOfB=0
#You only need to loop through once for A and B, no need to do each separately
#You can still use the old one though if you want to
    for number in inputList:
        if number == int(A):
            NumberOfA += 1
        if number == int(B):
            NumberOfB += 1
    listAB =[NumberOfA, NumberOfB]
    return listAB

var =raw_input("Enter the values on the faces of the cube seperated by commas: ").split(",")
inputList = [int(i) for i in var]
NA,NB = input_for_faces(*inputList)
print(input_for_faces(*inputList))
print("The probability of the chef winning is "+str((int(NA)/int(N))*(int(NB)/int(N))))