结果是正确的,但格式不正确

时间:2017-06-25 03:47:25

标签: python-3.x while-loop range do-while sqrt

我正在做作业,答案正确回归,但我需要他们说5! = 120而不是= 120。我该怎么做呢?

def getInt():
    getInt = int
    done = False

    while not done:
        print("This program calcultes N!")

        # get input for "N
        N = int(input("Please enter a non-negative value for N: "))

        if N < 0:
            print("Non-Negative integers, please!")
        else:
            done = True
        return N

def main():
    n = getInt()

    for i in range(n-1):
        n = n * (i+1)

    print("=" ,n)

main()

3 个答案:

答案 0 :(得分:0)

你可以告诉我你的作业说明吗? 我不确定你的例子中第一个值是什么..当前迭代或输入的原始数字是什么?

# declare getInt()
def getInt():

  getInt = int
  done = False

  while not done:

    # write "this program calculates N!"
    print("This program calcultes N!")

    # get input for "N
    N = int(input("Please enter a non-negative value for N: "))

    # if N < 0 then
    if N < 0:

      print("Non-Negative integers, please!")

    # else        
    else:

     # done = true
     done = True

    # return N
    return N

# main
def main():

  n = entry = getInt()

  for i in range(n-1):

    n = n * (i+1)

  print("{0}! = {1}".format(entry, n))

main()

结果:

/*
This program calcultes N!
Please enter a non-negative value for N: 5
5! = 120
*/

答案 1 :(得分:0)

我希望此代码有用。 print('Enter a positive integer') a = int(input()) def factorial(n): if n == 0: return(1) if n == 1: return(1) if n > 1: return(n * factorial(n-1)) if a < 0: print('Non-Negative integers, please!') if a >= 0: print(str(a) + '! = ' + str(factorial(a)))

答案 2 :(得分:0)

- (float)tempo { OSStatus err = noErr; Float32 value = 1.0; err = AudioUnitGetParameter(tempoUnit, kNewTimePitchParam_Rate, kAudioUnitScope_Global, 0, &value); if (err != noErr) { // -10878 NSLog(@"Could not get tempo unit rate: %d", err); } return value; } 中你可以使用另一个整数而不是n只是为了确保事情不会搞砸,你可以打印像joel所说for i in range(n-1)而不是n整数你会使用