python如何定义名称_Python:“未定义名称”

所以前几天我做了一个家庭作业的学费计划作业。这是我的节目。我希望这是不言而喻的。(顺便说一下,这是Python。)def print_intro():

print ("Welcome to the tuition program. This program will calculate your tuition cost.")

def get_id_num():

grad_status = input("Is the student an undergrad or grad (U/G)? ")

id_num = int(input("Enter the student's ID Number: "))

while (grad_status == "U") and (id_num < 0):

print("Sorry, that is invalid. Please try again.")

id_num = int(input("Enter the student's ID Number: "))

while (grad_status == "G") and (id_num >= 0):

print("Sorry, that is invalid. Please try again.")

id_num = int(input("Enter the student's ID Number: "))

def get_info():

get_id_num()

age = int(input("Enter the student's age (numbers only): "))

residency = input("Is the student in-state or out-of-state (I/O)? ")

num_credits = int(input("How many hours of credit will the student take? "))

correct = input("Is all the above information correct (Y/N)? ")

while correct == "N":

print("Please enter the information correctly!")

get_info()

else:

get_base_tuition()

def get_base_tuition():

if (grad_status == "U") and (num_credits <= 6) and (residency == "I"):

initial_tuition = 2000

if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "I"):

initial_tuition = 3000

if (grad_status == "U") and (num_credits >= 12) and (residency == "I"):

initial_tuition = 3800

if (grad_status == "U") and (num_credits <= 6) and (residency == "O"):

initial_tuition = 2000

if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "O"):

initial_tuition = 3000

if (grad_status == "U") and (num_credits >= 12) and (residency == "O"):

initial_tuition = 9000

if (grad_status == "G") and (num_credits <= 6) and (residency == "I"):

initial_tuition = 2000

if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "I"):

initial_tuition = 2500

if (grad_status == "G") and (num_credits >= 12) and (residency == "I"):

initial_tuition = 4400

if (grad_status == "G") and (num_credits <= 6) and (residency == "O"):

initial_tuition = 2000

if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "O"):

initial_tuition = 3700

if (grad_status == "G") and (num_credits >= 12) and (residency == "O"):

initial_tuition = 4400

print(initial_tuition)

所以在我运行这个函数之后,get_info()函数运行良好,直到它转到get_base_tudio()函数为止。我得到这个错误:line 28, in get_base_tuition

if (grad_status == "U") and (num_credits <= 6) and (residency == "I"):

NameError: name 'grad_status' is not defined

我应该在函数中添加参数吗?我被卡住了,真的不知道该怎么办。我知道,如果我在get-base-tuscience()中包含变量“grad-status”、“num-credits”和“residentity”,或者在运行get-base-tuscience()之前在Python Shell中定义变量,那么它当然可以工作。但如果我像现在一样把它放在另一个学费里,那就行不通了。有什么方法可以让get_base_tuscience()函数运行而不必将变量放在get_base_tuscience()中?因为我已经把这些变量放在函数get_id_num()和get_info()中了。

谢谢你的帮助。

如果你想知道基本学费的计算方法,我把作业附在这里:

Assignment Page 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值