简体   繁体   English

我的代码逻辑遇到问题

[英]I am facing problem with logic of my code

I am new to coding and was trying to solve question by codechef我是编码新手,正在尝试通过 codechef 解决问题

Problem Code = AUCTION问题代码 = 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). Alice 出价 AA 卢比,Bob 出价 BB 卢比,Charlie 出价 CC 卢比(其中 AA、BB 和 CC 是不同的)。 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我用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 .您根本不使用第一个highest值(我猜这不是您想要的),第二个最高值是在构造a or b or c上计算,它返回的第一个值评估为True

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

That's why highest always is equal to "Alice"这就是为什么highest总是等于"Alice"

Being you, to solve that task I would use just if作为你,为了解决我会使用的任务, 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:因此,您在此处将输入映射到 a、b、c:

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"但是你也给 a, b, c 不同的字符串值: 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:你应该做的是检查输入中的哪个值更大(a或b或c)相应地打印名称,如下所示:

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.也看看真值表:可以帮助你解释条件语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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