以下代码不会运行,也不会显示错误消息。请帮忙。另外,我如何添加一个只显示此代码输出的函数?
def main():
s= ""
phrase=""
programdescription(s)
userinput(phrase)
#This function displays the program description
def programdescription(s):
s = print("This program determines if a word, phrase, or sequence can be read the same backward as forward.")
#This function requests user input for analysis
def userinput(phrase):
phrase = input("Enter a word or phrase: ")
def s_phrase(phrase):
phrase = phrase.upper()
strippedPhrase = ""
for char in phrase:
if (48 <= ord(char) <= 57) or (65 <= ord(char) <= 90):
strippedPhrase += char
flag = True
n = len(strippedPhrase)
for j in range(int(n / 2)):
if strippedPhrase[j] != strippedPhrase[n - j - 1]:
flag = False
break
if flag:
print(phrase, "is a palindrome.")
else:
print(phrase, "is not a palindrome.")
main()
答案 0 :(得分:0)
好的,问题1是你从不打电话给s_phrase。 问题2是s_phrase无法看到短语变量。 问题是C是你的缩进搞砸了。 问题4更多的事实是,这似乎是一种解决挑战的非常“C”方式。从Spade借用,这是一种更简洁的方式,可以根据原始程序进行格式化。
def main():
s= ""
phrase=""
programdescription(s)
s_phrase(phrase)
#This function displays the program description
def programdescription(s):
s = print("This program determines if a word, phrase, or sequence can be read the same backward as forward.")
#This function requests user input for analysis
def s_phrase(phrase):
phrase = input("Enter a word or phrase: ")
phrase = phrase.upper()
r_phrase = phrase[::-1]
print(r_phrase)
if phrase == r_phrase:
print(phrase, "is a palindrome.")
else:
print(phrase, "is not a palindrome.")
main()