我正在尝试创建一个函数,您可以在单词"banana"中添加诸如"ana"之类的短语,并计算它在单词中找到短语的次数.我找不到我正在制作的一些测试单元无法正常工作的错误.
def test(actual, expected):
""" Compare the actual to the expected value,
and print a suitable message.
"""
import sys
linenum = sys._getframe(1).f_lineno # get the caller's line number.
if (expected == actual):
msg = "Test on line {0} passed.".format(linenum)
else:
msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'.".format(linenum, expected, actual))
print(msg)
def count(phrase, word):
count1 = 0
num_phrase = len(phrase)
num_letters = len(word)
for i in range(num_letters):
for x in word[i:i+num_phrase]:
if phrase in word: …Run Code Online (Sandbox Code Playgroud) 好吧,所以这里是我的代码,我得到了我想要的结果,但我不断得到它下面的"无"值.如何消除"无"值?
n = input("What day of the week are you leaving?")
r = input("How many days will you be resting?")
def days(n):
if n == 0:
print "Sunday"
elif n == 1:
print "Monday"
elif n == 2:
print "Tuesday"
elif n == 3:
print "Wednesday"
elif n == 4:
print "Thrusday"
elif n == 5:
print "Friday"
elif n == 6:
print "Saturday"
elif n >= 7:
print days(n%7)
print days(n+r)
Run Code Online (Sandbox Code Playgroud) 我似乎无法正常工作。当我输入3代表a,2代表b和3.61代表c。这样可行。但是,当我以其他顺序尝试这些值时(例如:a为3.61,b为3,c为2),它将返回false。我不知道是什么问题。提前致谢!
a = input("Enter a ")
b = input("Enter b ")
c = input("Enter c ")
def isright_angled():
if abs((a**2+b**2)-(c**2)) < 0.1 or abs((c**2-a**2)-(b**2)) < 0.1 or abs((c**2-b**2)-(a**2)) < 0.1:
return True
else:
return False
print isright_angled()
Run Code Online (Sandbox Code Playgroud)