0

I am a complete beginner at learning python 3. I came across a problem. Please take a look at the code within the three stars:

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

This method of input for *args didn't give the right output(It worked but gave wrong answer). But when I gave direct values for args, the program worked correctly.

Direct input refers to:

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

Please tell me what I did wrong.

Koushik Sahu
  • 99
  • 1
  • 1
  • 9
  • They're different because a single string argument `("1,1,1,1,1")` is fundamentally different to multiple arguments `(1, 1, 1, 1, 1)`. – jonrsharpe Jun 13 '18 at 07:19
  • How can I correct it? How can I convert a string argument to multiple arguments? – Koushik Sahu Jun 13 '18 at 07:20
  • You *convert* the input to what you actually need, starting with e.g. https://stackoverflow.com/q/7844118/3001761. – jonrsharpe Jun 13 '18 at 07:22
  • Doesn't explain how to convert a string argument to multiple arguments. Can you please answer by writing the correct code. – Koushik Sahu Jun 13 '18 at 07:23
  • So you do research for that next step. No, I'm not going to write your code for you. – jonrsharpe Jun 13 '18 at 07:27
  • The link you added explained the way to convert it to list or tuple. – Koushik Sahu Jun 13 '18 at 07:29
  • Yes, that's correct. That's the first step. There are other steps; you can find the duplicates for those yourself. Please note that SO isn't a tutorial or code-writing service, decomposing the problem into steps and solving them is a key part of programming. – jonrsharpe Jun 13 '18 at 07:34

2 Answers2

1

1, In your code in this part in the 'if' condition you are comparing a string to an integer so that condition will become false and does not count 'NumberOfA'

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

output:

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, you are passing single string as input for *args so to pass multiple arguments you have convert input as list lvar=var.split(',') and then by using *lvar in function call {NA, NB=input_for_faces(*lvar)}, will pass multiple arguments one by one from the list

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

output:

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

Sky
  • 36
  • 3
0

If you don't want to or have to use *args, you could do this:

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

or if you have to use *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))))
user3451660
  • 447
  • 8
  • 17