python 3x程序无法正确返回/显示变量值

时间:2016-04-08 01:59:12

标签: python-3.x

问题已得到修复,如果我忘记了任何其他重要信息,请对不起。

我正在努力解决为什么我的课程无法计算学费。 这是我的代码。 它是要求用户输入一个工作正常的代码类型(pyhton,C ++或java) 然后它要求用户输入他们想要参加的课程/额外时间和测试的数量,这也是我能说的工作正常。 我相信feecalc函数导致问题,但我在运行它时没有遇到任何实际错误,除了当feecalc函数打印结果时,它打印字符串,但变量没有值?或者它的价值已经改变但我不确定为什么会发生这种情况。

我为粘贴大量代码而道歉,但我不确定导致此问题的原因与它运行完全相同'罚款。这是一个逻辑错误还是我犯了一个我忽略的错误?

test data   
code = python  
number of lessons = 2  
additional hours = 3  
tests = 1  

输出如下

 Welcome to Dr Ho Coaching Centre fee calculator
what code do you wish to study?
Python - Java - C++
>python
thank you for choosing python
How many lessons would you like?2
How many additional hours would you like?3
how many tests would you like to sit?1

python
Tuition fee for the lessons is $ 
Tuition fee for the additional hours is $ 
Tuition fee for the total tests is $ 
Total Tuition fee is $ 
Would you like to calculate the Tuition fee for another student?

如图所示,没有计算出的价格显示。我已经从测试数据中删除了一些打印的字符串,因为我无法在此论坛中正确格式化,对不起,如果这改变了什么。

def body( ):
    codeofstudy = ''
    tlessons = ''
    xhours = ''
    tests = ''
    totalcost = ''
    lessoncost = ''
    xhourscost = ''
    testscost = ''
    print('''
-------------------------------------------------
Welcome to Dr Ho Coaching Centre fee calculator
-------------------------------------------------''')
codeofstudy = codecheck(codeofstudy)
studytime(tlessons, xhours, tests)
print(tlessons)
feecalc(codeofstudy, tlessons, xhours, tests, lessoncost, xhourscost, testscost, totalcost)
print('Tuition fee for the lessons is $',lessoncost)
print('Tuition fee for the additional hours is $', xhourscost)
print('Tuition fee for the total tests is $', testscost)
print('Total Tuition fee is $', totalcost)
rerun()



def codecheck(codeofstudy):
codeofstudy = input('''what code do you wish to study?
Python - Java - C++
>''')
if codeofstudy in {"python", "Python", "Java", "java", "C++", "c++"}:
    print('thank you for choosing', codeofstudy)
    return codeofstudy
else:
    print('That is not a valid answer')
    codecheck(codeofstudy)


def studytime(tlessons, xhours, tests):
tlessons = input('How many lessons would you like?')
xhours = input('How many additional hours would you like?')
tests = input('how many tests would you like to sit?')
tlessons = int(tlessons)
xhours = int(xhours)
tests = int(tests)
return tlessons, xhours, test


def feecalc(codeofstudy, tlessons, xhours, tests, lessoncost, xhourcost, testscost, totalcost):
if codeofstudy in ("python", "Python"):
    print('python')
    lessoncost = tlessons * 300
    xhourscost = xhours * 200
    testscost = tests * 250
    totalcost = lessoncost + xhourscost + testscost
    return lessoncost, xhourscost, testscost, totalcost

elif codeofstudy in ("java", "Java"):
    print('java')
    lessoncost = tlessons * 200
    xhourscost = xhours * 150
    testscost = tests * 150
    totalcost = lessoncost + xhourscost + testscost
    return lessoncost, xhourscost, testscost, totalcost

elif codeofstudy in ("c++", "C++"):
    print('C++')
    lessoncost = tlessons * 175
    xhourscost = xhours * 175
    testscost = tests * 170
    totalcost = lessoncost + xhourscost + testscost
    return lessoncost, xhourscost, testscost, totalcost

def rerun():
rerun = ' '
rerun = input('Would you like to calculate the Tuition fee for another student?')
if rerun in ("y", "Y"):
    body()
body()

提前感谢您的任何帮助=]

1 个答案:

答案 0 :(得分:0)

您使用值空字符串''声明了所有变量,然后使用默认值打印它们。 studytime()将值分配给3个局部变量(范围仅属于方法),在studytime()之外声明的变量tlessons,xhours和test未分配任何新值。

您需要将代码更改为:

tlessons, xhours, test = studytime(tlessons, xhours, tests)

返回值并将它们分配给相应的变量

例如

def test(x):
    x = 10
    return x



x = 0
test(x)
print(x)  # output is 0

x = test(x)
print(x)  # output is 10