python - 用于 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
解决方案
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()
推荐阅读
- r - csv read 在 R.. 中向我加载的数据框插入一个“X”列?
- sql - SSIS 将数据从 SQL db 复制到同一 excel 目标上的多个选项卡
- powershell - 如何在组 AD PowerShell 中获取组的用户
- android - 如何在 Android Developer Console 崩溃报告中读取“OR 调用堆栈”?
- html - 引导元素未隐藏
- ios - 是否需要签署付费应用程序协议才能在 iOS 上启用初始 IAP 测试?
- ios - 在 Swift 4 IOS 中将 XML 数据转换为字符串
- c# - C# DataTable DataGrid 列名中的特殊字符“/”(斜杠)
- typescript - 在量角器中,很少有代码行没有间歇性地执行
- .net-core - 无法在 .net 核心中返回已完成的成功任务