正如下面的doc字符串所述,我正在尝试编写一个带有3个参数(浮点数)并返回一个值的python代码.例如,输入低1.0,hi为9.0,分数为0.25.这将返回3.0,这是1.0和9.0之间的数字的25%.这就是我想要的,下面的"返回"等式是正确的.我可以在python shell中运行它,它给了我正确的答案.
但是,当我运行此代码以尝试提示用户输入时,它会一直说:
"NameError:名称'低'未定义"
我只想运行它并获得提示:"输入low,hi,fraction:"然后用户输入,例如,"1.0,9.0,0.25"然后它将返回"3.0".
我如何定义这些变量?如何构造print语句?我如何让它运行?
def interp(low,hi,fraction): #function with 3 arguments
""" takes in three numbers, low, hi, fraction
and should return the floating-point value that is
fraction of the way between low and hi.
"""
low = float(low) #low variable not defined?
hi = float(hi) #hi variable not defined?
fraction = float(fraction) #fraction variable not defined?
return ((hi-low)*fraction) +low #Equation is correct, but can't get
#it to run after I compile it.
#the below print statement is where …Run Code Online (Sandbox Code Playgroud) 我有以下python代码几乎适合我(我很接近!).我有一个正在开放的莎士比亚戏剧的文本文件:原始文本文件:
"但通过那个窗户打破了光线
它是东部,朱丽叶是太阳
太阳公平,杀死羡慕的月亮
谁已经病了,脸色苍白悲伤"
我给我的代码的结果是这样的:
['升起','但','它','朱丽叶','谁','已经','和','和','和','打破','东','羡慕','公平','悲伤','是','是','是','杀','轻','月亮','苍白','病态','软','太阳','太阳' ,'the','the','the','through','what','window','with','yonder']
所以这几乎就是我想要的:它已经按照我想要的方式排列在列表中,但是如何删除重复的单词呢?我正在尝试创建一个新的ResultsList并将单词附加到它,但它给了我上面的结果,而没有删除重复的单词.如果我"打印结果列表",它只会丢弃大量的单词.我现在的方式已接近,但我想摆脱额外的"和","是","太阳"和"'s"....我想保持简单并使用append(),但我不知道如何才能让它发挥作用.我不想对代码做任何疯狂的事情.为了删除重复的单词,我在代码中遗漏了哪些简单的东西?
fname = raw_input("Enter file name: ")
fhand = open(fname)
NewList = list() #create new list
ResultList = list() #create new results list I want to append words to
for line in fhand:
line.rstrip() #strip white space
words = line.split() #split lines of words and make list
NewList.extend(words) #make the list from 4 lists to 1 list
for word in line.split(): #for each word in line.split()
if words not in line.split(): #if …Run Code Online (Sandbox Code Playgroud)