0

我似乎无法弄清楚为什么我不能让定义的函数工作。我对python很陌生,但感觉它应该按原样工作。

import math
import random
answer = 0 
# i *think* this means that the value of the variable answer in the function
# ma_formula() can be used outside the function once the function is done right?
def my_forumla():
    answer = a * b * c;

a = math.pi;
b = random.randint(2,288);
c = eval(input("Enter your number here: \n"));
print(str(a) + ", " + str(b) + ", " + str(c)) 
my_formula();
print(answer);
4

1 回答 1

3

拼错了函数名称:

def my_forumla():

应该:

def my_formula():

你调换了uand m

你在你的问题标题和评论中再次这样做了,你拼写的地方ma_formula(注意a开头的不是 a y)。

请注意,这answer是一个局部变量,my_formula()无法在函数外部访问。使用return它来代替它并分配返回值或直接打印它。也给你的函数参数,这是比把a,bc作为全局变量更好的做法:

def my_forumla(a, b, c):
    return a * b * c;

answer = my_formula(a, b, c)
print(answer)
于 2013-11-10T22:32:56.493 回答