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



我是学习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))))

请告诉我我做错了什么。

1,在本部分的代码中,在"if">条件中,您将字符串与整数进行比较,以便该条件将变为假并且不计算"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(','(,然后在函数调用 {NA, NB=input_for_faces(*lvar(} 中使用*lvar,将从列表中一个接一个地传递多个参数

s = input("输入 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

如果你不想或必须使用 *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))))

或者,如果您必须使用 *参数:

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))))

最新更新