2016-05-02 33 views
-2

我正在创建一个程序来解决测试案例#1中提出的问题。但是我总是在测试用例#2中得到错误的答案,这是因为我从来不知道测试用例#2是什么!为什么我无法在Hackerrank中获得正确答案? - Python 3

为什么我在第二种情况下出错?

这里的问题是propsed我的回答:

任务 完成代码在下面的编辑器。变量i,d和s已经为您声明和初始化。您必须声明3个变量:int类型,double类型之一和String类型之一。然后你必须从标准输入读取3行输入并初始化你的3个变量。最后,您必须使用+运算符执行以下操作:

在新行上输出i加上您的int变量的总和。 将d加上双倍变量的总和打印到新行上的小数点后一位。 用读取的字符串连接s作为输入,并将结果打印在新行上。

测试案例#1 - 输出预计:

8.0

HackerRank是学习和实践编码的最佳选择!

+测试案例#2 - 输出预计:

6.8

是我最喜欢的plataform!

这里是我的代码:

def test_case_1(): 
     ii = int() 
     dd = float() 
     ss = str() 
     i = 4 
     d = 4.0 
     s = 'HackerRank ' 
     ii = 12 
     dd = 4.0 
     ss = "is the best place to learn and practice coding!" 
     total_int = i + ii 
     print (total_int) 
     total_double = d + dd 
     print(total_double) 
     print (s + ss) 
     memory = open('memory.txt', 'w') 
     memory.write("1") 
     memory.close() 
def test_case_2(): 
     ii = int() 
     dd = float() 
     ss = str() 
     i = 4 
     d = 4.0 
     s = 'HackerRank ' 
     ii = 3 
     dd = 2.8 
     ss = "is my favorite plataform!" 
     total_int = i + ii 
     print (total_int) 
     total_double = d + dd 
     print(total_double) 
     print (s + ss) 
     memory = open('memory.txt', 'w') 
     memory.write("0") 
     memory.close() 
def starting(): 
    memory = open('memory.txt', 'r') 
    ted = [] 
    for line in memory: 
     ted.append(line) 

    memory.close() 
    return ted 

def test(ted, main): 
    if ted == ['0'] : 
     test_case_1() 

    elif ted == ['1'] : 
     test_case_2() 


def main(): 
    try: 
     memory = open('memory.txt', 'r') 
     memory.close() 

    except: 
      memory = open('memory.txt', 'w') 
      memory.write("0") 
      memory.close() 


    pronto = starting() 
    test(pronto, main) 


main() 
+1

我没有看到在您需要写入一个文件 –

+0

你们是不是只是硬编码测试用例回答问题描述在哪里呢?这不是如何工作。 – user2357112

回答

2

你得太多the problem。您需要从标准输入中读取样本输入。

它也说变量i,d和s已经为您申报和初始化,因此您不应该对这些值进行硬编码。

此代码可以正常工作,但请确保您真正理解它,而不仅仅是复制粘贴。

import sys 

i2 = int(sys.stdin.readline()) 
d2 = float(sys.stdin.readline()) 
s2 = sys.stdin.readline() 

# Print the sum of both integer variables on a new line. 
print(i+i2) 
# Print the sum of the double variables on a new line. 
print(d+d2) 
# Concatenate and print the String variables on a new line 
# The 's' variable above should be printed first. 
print(s+s2) 
1

谢谢cricket_007!为了真实,我理解并在Python中完成自己的代码。看:

def printing(): 
 
     i = int() 
 
     d = float() 
 
     s = str() 
 
     ii = 4 
 
     dd = 4.0 
 
     ss = 'HackerRank ' 
 
     i = int(input()) 
 
     d = float(input()) 
 
     s = str(decide(i)) 
 
     total_int = i + ii 
 
     print (total_int) 
 
     total_double = d + dd 
 
     print(total_double) 
 
     print (ss + s) 
 
     memory = open('memory.txt', 'w') 
 
     memory.write("0") 
 
     memory.close() 
 

 
def decide(val): 
 
    if val == 12 : 
 
     quote = "is the best place to learn and practice coding!" 
 

 
    elif val == 3 : 
 
     quote = "is my favorite platform!" 
 
     
 
    return quote 
 

 

 
def main(): 
 
    memory = open('memory.txt', 'w') 
 
    memory.write("0") 
 
    memory.close() 
 
    printing() 
 

 

 
main()

+0

你甚至没有使用'decide()'函数。 “内存”文件毫无意义。最后,Python不需要'main()'方法 –

相关问题