def main():
print('Period of a pendulum')
Earth_gravity = 9.8
Mars_gravity = 3.7263
Jupiter_gravity = 23.12
print(' ')
pen = float(input('How long is the pendulum (m)? '))
if pen < 0:
print('illegal length, length set to 1')
else:
print(' ')
main()
我想要做的是输入的值是否为负值,将其设置为“1”并显示消息“非法长度,长度设置为1”。如果它是一个正数,那么我会用它来计算摆锤摆动的周期。我收到“if pen&lt; 0时的语法错误消息: builtins.NameError:名称'pen'未定义“谢谢!
答案 0 :(得分:2)
-- supposing a page size of 10 and pagination order by message_id
select message_id, ceiling(message_number/10) as page
from (
select message_id, @row_number:=@row_number+1 AS message_number
from forum_messages, (select @row_number:=0) AS row_number
order by message_id
) as numbered_messages
where message_id=<the id of the message>
试试这个:pen变量在函数之外,因此在if语句中无法访问它
答案 1 :(得分:0)
首先定义一个定义pen
的方法:
def main():
print('Period of a pendulum')
Earth_gravity = 9.8
Mars_gravity = 3.7263
Jupiter_gravity = 23.12
print(' ')
pen = float(input('How long is the pendulum (m)? '))
但根据缩进,您现在尝试在pen
方法之外使用main
:
if pen < 0:
print('illegal length, length set to 1')
else:
print(' ')
main()
您可以将if/else
移至main
方法或完全删除main
定义。