有没有办法用(1/2)的函数来代替(输入)零?

2024-09-29 17:15:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将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)

Tags: 函数ininput定义initnpresultfloat
2条回答

所以这里有一个代码可以满足你的需要。你知道吗

老实说,这很管用,但我不明白你为什么这么做。你做了一堆奇怪的计算,比如用同一个数乘和除。。。你知道吗

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...

声明变量时,可以使用一行程序:

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

我希望我以前的答案编辑完全回答你的问题,更多的澄清,我将在评论上提供

相关问题 更多 >

    热门问题