简体   繁体   中英

I am facing problem with logic of my code

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM