所以前几天我做了一个家庭作业的学费计划作业。这是我的节目。我希望这是不言而喻的。(顺便说一下,这是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