计算数字重复次数的简单程序
Simple program for counting number of repetition of a number
我完全是初学者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(',') 然后使用 *lvar函数调用 {NA, NB=input_for_faces(*lvar)}, 将从列表
中一一传递多个参数
s = 输入("Enter the value of N A B with white space in between ")
N、A、B=列表(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))))
或者如果您使用*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))))
我完全是初学者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(',') 然后使用 *lvar函数调用 {NA, NB=input_for_faces(*lvar)}, 将从列表
中一一传递多个参数s = 输入("Enter the value of N A B with white space in between ") N、A、B=列表(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))))
或者如果您使用*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))))