我正在尝试将0的值替换为.5或在最初输入时将其替换为1/2。
例如,我正在尝试在添加功能之前完成此操作。我只需要为输入重新定义0的值,并且只为0本身的单个实例重新定义。不是10+的值。
这是项目信息:
IN = input("Enter IN: ")
N = input("Enter N: ")
NP = input("Enter NP: ")
### These two lines are the part I can't get to work:
if digit == float(0):
digit = float(.5)
###
init = (float(IN)*(float(1)/float(2)))
baselimiter = - (float(N)*(float(1)/float(2))) + ((float(IN)*
(float(1)/float(2))) * (float(NP)*(float(1)/float(2))))
lset = init + baselimiter
limitconverto1 = (lset / init) * (init / lset)
infalatetoinput = (((init * float(IN))) / init )
limit = limitconverto1 * infalatetoinput
result = limit
print(result)
答案 0 :(得分:0)
声明变量时可以使用单行代码:
IN = (float(input("...")) if float(input("...")) != 0 else .5)
单线是for
循环或if
语句(或两者),它们在声明变量时排成一行,而不是多行。它们只能用于变量的声明。我建议的单线是多行:
if float(input("...")) != 0:
IN = float(input("..."))
else:
IN = .5 #You don't need to say float(.5) since .5 is a float anyway.
有关单行者的更多信息:One-Liners - Python Wiki
我希望对以前的回答进行修改后能完全回答您的问题,有关更多说明,请访问我的评论
答案 1 :(得分:0)
这是执行您想要的代码。
现在说实话,它可以工作,但我不明白您为什么这样做。您进行了一系列奇怪的计算,例如用相同的数字相乘和相除...
IN = float(input("Enter IN: "))
N = float(input("Enter N: "))
NP = float(input("Enter NP: "))
# The part that interests you.
IN = 0.5 if IN == 0 else IN
N = 0.5 if N == 0 else N
NP = 0.5 if NP == 0 else NP
init = IN * 1/2
baselimiter = -N*1/2 + IN*1/2*NP*1/2 # Removed all the superfluous float() and parenthesis.
lset = init + baselimiter
limitconverto1 = (lset / init) * (init / lset) # That's just always 1. What is intended here?
infalatetoinput = (((init * float(IN))) / init ) # That's always IN. Same question?
limit = limitconverto1 * infalatetoinput # Equivalent to 1 x IN...
result = limit
print(result) # Your result is always IN...