打印功能时不规则显示

时间:2016-07-09 19:27:31

标签: python string python-2.7

我正在尝试编写一个脚本,当调用时会提示用户输入一个字符串,它会向前,向后显示字符串,测试2个字符串是否相同,并测试字符串是否为回文。我想在输入菜单之前测试各个函数的正确性,但是调用palindrome()函数时的输出似乎是不规则的。有关为什么palindrome()函数中的两个语句以这种方式打印的任何帮助都将受到赞赏。

string = raw_input('Enter a string\n>>>')

def forward():
    for i in string:
        print i,
    return str(i)

def backwards():
    back = len(string) - 1
    while back > 0:
        print string[back],
        back -= 1
    return str(string[back])

def match():
    next_string = raw_input('Enter another string')
    if string.upper() == next_string.upper():
        print 'the strings ', string, ' and ', next_string, ' match.'
    else:
        print 'the strings ', string, ' and ', next_string, ' do not match.'

def palindrome():
    print backwards() # When I don't print this call it only displays the last value in the string
    y = forward() # When I do print this call it displays the last value in the string twice
    # This is the only way where these calls are opposite

palindrome() 

0 个答案:

没有答案