函数:y(x)=ln1/(1-x)
我如何编写Python程序来计算用户指定的x值的上述函数,其中ln是自然对数(以e为底的对数)?我将强制使用while
循环,以便程序对输入到程序中的x的每个合法值重复计算。当输入非法值“x”时,我使用break
终止程序
我已尝试使用以下代码,但似乎运行不正常:
import math
n = int(input("Enter the number to be converted: "))
while n >= 0:
if n <= 0:
break
print("Your number is not positive terminating program.")
x = math.log(n) * 1/(1-n)
print("The Log Value is:", x)
所以实际上,在while循环中没有合适的行
尝试:
首先,您的程序可能陷入无限循环;例如,如果输入
n = 1
,则n >= 0
为真n <= 0
为假,因此程序无限期地运行while
循环函数的“合法输入”必须是(实)数(严格地)小于1。如果
n == 1
,那么您正在进行零除运算。如果n > 1
,则在log函数中输入一个负数在建议的代码中,我只检查输入的“数字合法性”;i、 例如,输入一个空字符串将抛出一个错误。但我认为这超出了你在作业中的要求
相关问题 更多 >
编程相关推荐