首页 > 解决方案 > 用于 Def 函数的 Python 用户输入,我需要询问一个数字,然后使用该数字来倒数或递增

问题描述

我现在这是不对的,但我在正确的道路上吗?我对此超级陌生。任何帮助表示赞赏。任务是创建一个允许用户输入数字的脚本。然后如果输入 1,则打印从该数字到零的倒计时。如果输入 2,则打印数字的阶乘。

num = int(input("Enter a number greater than 1: "))
if num<0:
print("please enter number greater than zero");

elif(num==0):
print("Please enter number greater than 0");
else:
select = int(input("Enter 1 to countdown from or enter 2 to get the factorial"))
select = true
  

def factorial():
factorial=1;  
if num<0:
print("Factorial does not defined for negative integer");

elif(num==0):
print("The factorial of 0 is 1");
else:
while(num>0):
factorial=factorial*num
num=num-1
    
print("factorial of the given number is: ")
print(factorial)

def countdown():
countDown = num
while (countDown >= 0):
print(countDown)
countDown = countDown - 1
if countDown == 0:
print("Done!")
break

标签: pythonfunctioninput

解决方案


Figured it out with a lot of help. Thanks!! I appreciate the help. Here is the final code:

def countdown(num):
 countdown_num = num
 print (countdown_num)
 while (countdown_num >= 0):
 print(countdown_num)
 countdown_num = countdown_num - 1
if countdown_num == 0:
    print("Done!")
    break

def factiorial(num):
    factorial_num = 1
    while num>0:
    factorial_num = factorial_num * num
    num = num - 1
     print(factorial_num)

def main():
    num = int(input("Enter a number greater than 1: "))
    if num<0:
      print("please enter number greater than zero")
    elif num==0:
      print("Please enter number greater than zero, not zero")
    else:
      select = int(input("Enter 1 to countdown from or enter 2 to get the factorial")) 
      if select==1: 
        countdown(num)
      if select==2:
        factiorial(num)
main()

推荐阅读