Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用if和else语句返回正确输出的代码_Python_Python 3.x - Fatal编程技术网

Python 使用if和else语句返回正确输出的代码

Python 使用if和else语句返回正确输出的代码,python,python-3.x,Python,Python 3.x,下面的代码没有为某些测试返回正确的输出。我曾尝试重新编写代码来解决此问题,但一直无法做到这一点。我正在寻求有关如何使我的代码返回正确输出的帮助: def get_translations(string): """convert string to list""" list1 = [] lines = string.splitlines() order = 1 if string.split(',')[0] == "englis

下面的代码没有为某些测试返回正确的输出。我曾尝试重新编写代码来解决此问题,但一直无法做到这一点。我正在寻求有关如何使我的代码返回正确输出的帮助:

def get_translations(string):
"""convert string to list"""
list1 = []
lines = string.splitlines()
order = 1 if string.split(',')[0] == "english" else -1

if 'english,māori' in string:
    for seperate_words in lines:
        list1.append(tuple(seperate_words.split(",")[::order])) 
    
if 'māori,english' in string:
    for seperate_words in lines:
        list1.append(tuple(seperate_words.split(",")[::order]))

else:
    print("Header language not recognized!")
    print("We can't play, hōhā!")
    return -1
    
if len(list1) >= 1:
    print("Kia ora, you have {0} terms to translate today :-)"\
          .format(len(list1) - 1))

number = 0
correct = 0
possible = (len(list1) - 1)

for num in range(len(list1[1:])):
    print("E: {}".format(list1[1+number][0]))
    answer = input("M: ")
    
    if answer == list1[1+number][1]:
        print("Ka pai")
        correct += 1
        number += 1
        
    else:
        print("A: {}".format(list1[1+number][1]))
        number += 1

total = (correct / possible) * 100
return total

def reo_test(string):
    """stuff"""
    return get_translations(string)
days_string = '''english,māori
Monday,Rāhina
Tuesday,Rātū
Wednesday,Rāapa
Thursday,Rāpare
Friday,Rāmere
Saturday,Rāhoroi
Sunday,Rātapu'''

score = reo_test(days_string)
print("Score {:.2f}".format(score))
这是返回错误输出的测试:

def get_translations(string):
"""convert string to list"""
list1 = []
lines = string.splitlines()
order = 1 if string.split(',')[0] == "english" else -1

if 'english,māori' in string:
    for seperate_words in lines:
        list1.append(tuple(seperate_words.split(",")[::order])) 
    
if 'māori,english' in string:
    for seperate_words in lines:
        list1.append(tuple(seperate_words.split(",")[::order]))

else:
    print("Header language not recognized!")
    print("We can't play, hōhā!")
    return -1
    
if len(list1) >= 1:
    print("Kia ora, you have {0} terms to translate today :-)"\
          .format(len(list1) - 1))

number = 0
correct = 0
possible = (len(list1) - 1)

for num in range(len(list1[1:])):
    print("E: {}".format(list1[1+number][0]))
    answer = input("M: ")
    
    if answer == list1[1+number][1]:
        print("Ka pai")
        correct += 1
        number += 1
        
    else:
        print("A: {}".format(list1[1+number][1]))
        number += 1

total = (correct / possible) * 100
return total

def reo_test(string):
    """stuff"""
    return get_translations(string)
days_string = '''english,māori
Monday,Rāhina
Tuesday,Rātū
Wednesday,Rāapa
Thursday,Rāpare
Friday,Rāmere
Saturday,Rāhoroi
Sunday,Rātapu'''

score = reo_test(days_string)
print("Score {:.2f}".format(score))
它应该返回(输入是每个M字母后面的单词):


这里的错误是
if'māori,english'字符串:
:if应该是
elif
。 目前,您在第一个分支
english,毛利人
中正确地构建了翻译列表,但是有一个单独的
if/else
块失败,因此错误被打印出来

干杯,祝你学习顺利