尝试在 python 中定义多个函数时出现语法错误? [英] Syntax Error when trying to define multiple functions in python?
问题描述
我正在尝试学习 Python,所以我只是在编写一些简单的程序.我写了这两段代码来定义我想在程序中使用的两个函数,它们都做他们想做的事,但是当我尝试将它们粘贴到 IDLE 时,它说第二个 def 存在语法错误.知道这是什么吗?
I'm trying to learn python, so I'm just writing some simple programs. I wrote these two bits of code to define two of the functions I want to use in the program, and they both do what they want but when I try to paste them into IDLE it says there is a syntax error at the second def. Any idea what this is?
代码如下:
def print_seq1(number):
number = input("Pick a number: ")
print " "
while number != 1:
if number%2==0:
print number
number = number/2
else:
print number
number = number*3 + 1
print number
print " "
choice = 0
def print_seq2(number):
number = input("Pick a number: ")
print " "
while number != 1:
if number%2==0:
print number,
number = number/2
else:
print number,
number = number*3 + 1
print number
print " "
choice = 0
推荐答案
交互式解释器(又名 REPL,只是解释器"和许多其他术语)通常只需要一个顶级语句(函数定义、类定义), 一个全局赋值, 一个循环, ...) 一次.你给它两个,它很困惑.尝试输入第一个 def
,一个空行以确认并实际运行您的输入,然后输入第二个 def
.
Interactive interpreters (a.k.a. REPL, just "interpreter", and many other terms) usually expect only a single top-level statement (a function definition, a class definition, a global assignment, a loop, ...) at a time. You give it two and it's confused. Try putting in the first def
, a blank line to confirm and actually run your input, then the second def
.
这篇关于尝试在 python 中定义多个函数时出现语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!