我正在创建一个程序来解决测试案例#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()
我没有看到在您需要写入一个文件 –
你们是不是只是硬编码测试用例回答问题描述在哪里呢?这不是如何工作。 – user2357112