【问题标题】:I am solving a question on hacker rank I don't know how to read stdinput我正在解决一个关于黑客等级的问题我不知道如何阅读 stdinput
【发布时间】:2019-12-14 17:25:02
【问题描述】:

尽管我的解决方案是正确的,但当我尝试在hackerrank 上解决此问题时,我无法弄清楚为什么它会显示错误。我无法理解 STDIN 和 STDOUT。请参考我附上的图片:

import sys

def triangle(my_string = sys.stdin.readline()):
    # first parse the standard input which is in form of string
    char = []
    
    for i in str(my_string):
        char.append(i)
                
    # is all the sides of the triangle are equal it is equilateral triangle
    if char[0] == char[1] == char[2]:
        print("Equilateral")
    
    # if two sides are equal it is an Isosoleces triangle 
    elif (char[0] == char[1]) or (char[1] == char[2]) or (char[2] == char[0]):
        print("Isosceles")
    
    # if all three sides are different it is an  Noneofthese 
    else:
        print("None of these")

【问题讨论】:

  • 你永远不会打电话给triangle
  • Please don't post screenshots of text。它们无法被搜索或复制,并且可用性差。相反,将代码作为文本直接粘贴到您的问题中。如果选择它并单击{} 按钮或Ctrl+K,则代码块将缩进四个空格,这将导致其呈现为代码。

标签: python stdin


【解决方案1】:

这是有效的代码。您必须将输入转换为整数。 您也忽略了输入中的空格。希望对您有所帮助。

import sys

my_string = input()
# first parse teh standard input wich is in form of string
splits = my_string.split(sep=' ')
value1 = int(splits[0])
value2 = int(splits[1])
value3 = int(splits[2])

# is all the sides of the triangle are equal it is eqaulatral triangle
if value1 == value2 == value3:
    print("Equilateral")

# if two sides are eqaul it is an isoleces triangle
elif (value1 == value2) or (value2 == value3) or (value3 == value1):
    print("Isosceles")

# if all three sides are diffrent it is an  Noneofthese
else:
    print("None of these")

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 2023-02-03
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 2021-09-15
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多