Home > Back-end >  I am facing problem with logic of my code
I am facing problem with logic of my code

Time:07-13

I am new to coding and was trying to solve question by codechef

Problem Code = AUCTION

Alice, Bob and Charlie are bidding for an artifact at an auction. Alice bids AA rupees, Bob bids BB rupees, and Charlie bids CC rupees (where AA, BB, and CC are distinct). According to the rules of the auction, the person who bids the highest amount will win the auction. Determine who will win the auction.

I wrote the code in python

t = int(input())
for i in range(t):
    n  = (a,b,c) = map(int,input().split())
    a , b , c = "Alice","Bobs","Charlie"
    highest = max(n, default = 0)
    highest = a or b or c
    print(highest)

My Input

200 100 400

155 1000 566

736 234 470

124 67 2

My Output

Alice
Alice
Alice
Alice

CodePudding user response:

Not sure what algorithm you invent to solve that task, but apparently the problem lies here:

    highest = max(n, default = 0)
    highest = a or b or c

You do not use the first highest at all (which I guess is not what you want) and second highest is calculating on construction a or b or c which returns first value that evaluates to True.

print(bool("Alice")) # prints True

That's why highest always is equal to "Alice"

Being you, to solve that task I would use just if

a, b, c = map(int, input().split())
if a > b:
    if a > c:
        print("Alice")
    else:
        print("Charlie")
else:
    if b > c:
        print("Bob")
    else:
        print("Charlie")

CodePudding user response:

So you are mapping inputs to a,b,c here:

n = (a,b,c) = map(int,input().split())

But then you also give to a, b, c different string values: a , b , c = "Alice","Bobs","Charlie"

What should you do is check which value from the input is bigger(a or b or c) the print the name accordingly like this:

if max(n)== a :
  
   print("Alice")

if max(n)== b :
  
   print("Bob")

if max(n)== c :
  
   print("Charlie")

CodePudding user response:

I think you have kind off overcomplicated the issue.

Go simple on those.

alice = int(input()), "Alice"
bob = int(input()), "Bob"
charlie = int(input()), "Charlie" 
print(max(alice, bob, charlie)[1],"has the highest bet.")

Also you should not reassign variables unless it is your point in more complex algo.

Also take a look at truth table: Can help you to explain condition statements.

  • Related