Python 3.x 它要求给出值,而不是给出答案。它一点也不给我
在这里,我创建了一个模块Python 3.x 它要求给出值,而不是给出答案。它一点也不给我,python-3.x,Python 3.x,在这里,我创建了一个模块 class Employee: def __init__(self): self.name = input("Enter your name: ") self.account_number = int(input("Enter your account number: ")) def withdraw(self): # it receives values from for if withdraw1 &
class Employee:
def __init__(self):
self.name = input("Enter your name: ")
self.account_number = int(input("Enter your account number: "))
def withdraw(self): # it receives values from for
if withdraw1 > current_balance:
print ("You have entered a wrong number: ")
else:
print ("The current balance is: ", current_balance - withdraw1)
我遇到的问题是,虽然它要求的是值,而不是给我一个答案,但它没有给我 以下是我对您的代码的看法
# TASK2.py
class Employee:
def __init__(self):
self.name = input("Enter your name: ")
self.account_number = int(input("Enter your account number: "))
# make sure you initialise your member variables!
self.withdraw_val = 0 # withdraw1 is ambiguous, so I use withdraw_val instead
self.current_balance = 0
# receives values from for ### no it doesn't, right now, it GIVES values TO your "for" function
def withdraw(self):
if self.withdraw_val > self.current_balance: # remember to use "self." to
# access members within the class
print ("You have entered a wrong number: ")
else:
# again, remember "self."
print ("The current balance is: ", self.current_balance - self.withdraw_val)
注意:从现在起,我将把你原来的功能称为for_employee。还要注意的是,我仍然不清楚你想要完成什么,而且很可能有一个更合适的名字来形容它 由于您原来的
for_employee
函数没有返回任何内容,因此默认情况下它返回None
。(这解释了您看到的输出。)
我认为你误解了函数的工作原理。比如说,
d = for_employee(c.withdraw())
print(d)
您对.draw()
方法的评论不准确
“它接收来自的值”
更准确地说,将首先计算c.draw()
,然后将返回的任何内容作为参数传递到for_employee
函数中。draw
方法“为”for_employee函数提供值,而不是“从接收值”
更合理的办法是
c.withdraw() # on a line by itself, since it doesn't return anything
d = for_employee(c) # pass the entire object, since you'll be using self.withdraw_val and whatnot
print(d)
另一个问题是传统命名。这就是我在为
>>> def for(a): return a
SyntaxError: invalid syntax
同样,for
在Python中是一个关键字,不要用它来命名变量、函数或类
使用self
,情况就不那么严重了(但我可以看出这让你感到困惑)self
更像是类方法中使用的一种约定。但是for_employee
不是类方法。因此,按照惯例,参数不应命名为self
(我觉得代码像意大利面条一样,如果您通过将
for_employee
方法移动到类本身中来重构代码,那么使用self
就完全有意义了)首先,它不要求值。方法
定义在哪里?为什么不使用“#”作为注释?我一开始确实使用了“#”,但在建议中,他们说我应该使用“>”我认为你误解了函数和返回语句。我建议在进行下一次任务之前,先阅读一篇教程,以便更好地理解它们。有关代码的其他问题,请参见我的答案。现在不在我的计算机后面,但是您可以在末尾的某个地方使用关键字为
添加一行吗?只是为了检查使用它作为函数名是否不会把Python搞得一团糟。你是对的,对于def for()
,我得到了无效语法。非常感谢。
c.withdraw() # on a line by itself, since it doesn't return anything
d = for_employee(c) # pass the entire object, since you'll be using self.withdraw_val and whatnot
print(d)
>>> def for(a): return a
SyntaxError: invalid syntax