Python I';我得到一个语法错误,冒号在“后”;如果(num<;0)"; num=float(输入('输入正数:')) 如果(num

Python I';我得到一个语法错误,冒号在“后”;如果(num<;0)"; num=float(输入('输入正数:')) 如果(num,python,Python,这里有很多错误 第1行缺少闭合括号 使用int比使用float更合适 第8行中的变量阶乘未初始化,但 引用本身 最后一行的格式不正确 请将其与以下有效代码进行比较: num = float(input('Enter a positive number: ') if (num < 0): print("Sorry, factorial does not exist for negative numbers") elif (num == 0): print("The fa

这里有很多错误

  • 第1行缺少闭合括号

  • 使用int比使用float更合适

  • 第8行中的变量阶乘未初始化,但 引用本身

  • 最后一行的格式不正确
请将其与以下有效代码进行比较:

num = float(input('Enter a positive number: ')
if (num < 0):
    print("Sorry, factorial does not exist for negative numbers") 
elif (num == 0):
    print("The factorial of 0 is 1.")
else:
   for i in range(1,num + 1):     
       factorial = factorial * i
   print("The factorial of",num,"is",factorial)
def阶乘(x):
如果x==1或x==0:
返回1
其他:
返回x*阶乘(x-1)
num=int(输入('输入正数:'))
如果(num<0):
打印(“对不起,负数不存在阶乘”)
其他:
fac=阶乘(num)
print(“+str(num)+”的阶乘是“+str(fac))

我已将阶乘的计算放入一个递归调用的函数中(您可以研究)。

问题中的缩进不应该被修复,这是错误的原因!第一行缺少一个括号。在
if
行中有一个括号溢出。
if num<0:
就足够了。
def factorial(x):
    if x == 1 or x == 0:
        return 1
    else:
        return x * factorial(x-1)

num = int(input('Enter a positive number: '))
if (num < 0):
    print("Sorry, factorial does not exist for negative numbers") 
else:
   fac = factorial (num) 
   print("The factorial of " + str(num) + " is " + str(fac))