一般来说,我是Python和编程的新手,我不知道我输入的是什么错误。
我正在尝试创建一个“倒计时和阶乘”程序,它允许我的用户输入一个数字,该数字将被用作倒计时或阶乘,这取决于1或2的第二个输入项。
如果他们选择1,那么程序将运行num的倒计时。如果他们选择2,那么它将运行num的阶乘。
我相信我已经创建了一个正确的脚本,但是,每次我尝试在IDLE中运行程序时,我都会在第13/14行得到一个“无效语法”的弹出消息。第13行是两个input行之间的空行。
谁能帮我弄清楚为什么会出现这个语法错误弹出窗口?代码如下:
import math
def countdown():
if num == 0:
return
print(num)
countdown(num-1)
def factorial():
print(math.factorial(num))
num = int(input('Please enter an integer greater than 1.\n')
userChoice = int(input('Please enter a either 1 or 2. If 1 is entered, a countdown from that number to zero is printed. If 2 is entered, the factorial of the number is printed.\n'))
if userChoice == '1':
countdown()
elif userChoice == '2':
factorial()发布于 2020-02-14 14:28:17
你的问题出在下面这一行:
num = int(input('Please enter an integer greater than 1.\n')
# ^_____^ ____^___
# open(2) close(1)您有两个左括号,但只有一个结束括号。它应该是:
num = int(input('Please enter an integer greater than 1.\n'))
# _^_
# addhttps://stackoverflow.com/questions/60123615
复制相似问题