[英]How to use if statements in classes
I'm trying to make a simple library where the user can add and remove books from his shopping cart, but I don't know how to use if statements with OOP and classes.我正在尝试创建一个简单的库,用户可以在其中添加和删除购物车中的书籍,但我不知道如何使用带有 OOP 和类的 if 语句。
try:
class library:
def __init__(self, books, customer):
self.books = books
self.customer = customer
# sign:
check = input("manager account(1), customer account(2): ")
if check == "2":
#age >= 18
age = int(input("enter your age: "))
if age >= 18:
#name
name = input("enter your firstname: ")
# ID
import random
x = "ID"+str(random.randint(101,999))
print(f"your ID is: {x}")
print("you should memorize it")
y = input("enter password that has at list 8 caracterse: ")
# Password
while len(y) < 8:
y = input("enter password that has at list 8 caracterse: ")
print(f"your password is: {y}")
print("you should memorize it")
data = [x,y]
choice_1 = input("check your shopping cart(1): \nadd books to your shopping cart(2): \nremove books from your shopping cart(3): ")
if choice_1 == "1":
def __str__(self):
return f"customer {self.customer} bought those books{self.books}"
elif choice_1 == "2":
def __iadd__(self, other):
self.books.append(other)
return self
order = library(["the golsen company"],"Mr.asad")
print(order.books)
order += input("enter a book: ")
print(order.books)
except ValueError as ages:
print(ages)
I don't know if this is the right way to use the if
statement with classes so if you can just give me an example to show how it's done correctly?我不知道这是否是将
if
语句与类一起使用的正确方法,所以如果你能给我一个例子来说明它是如何正确完成的?
when you said "fonction", I think you mean "function".当您说“功能”时,我认为您的意思是“功能”。 When you make a
try:
block, you must also add an except:
block.当您
try:
块时,您还必须添加一个except:
块。 Also, don't put code directly in the class.另外,不要将代码直接放在 class 中。 Put it inside a function (aka methods).
将其放入 function(又名方法)中。 I put the code starting from
# sign
in the __int__
method.我把从
# sign
开始的代码放到了__int__
方法中。 We generally don't put methods (functions) inside other methods.我们一般不会把方法(函数)放在其他方法里面。 So, put the
__str__
and __iadd__
methods outside the __init__ method
.因此,将
__str__
和__iadd__
方法放在__init__ method
之外。 To call it, use self.__str__()
and self.__iadd__()
.要调用它,请使用
self.__str__()
和self.__iadd__()
。
Here is the updated code:这是更新的代码:
try:
class library:
def __init__(self, books, customer):
self.books = books
self.customer = customer
# sign:
check = input("manager account(1), customer account(2): ")
if check == "2":
# age >= 18
age = int(input("enter your age: "))
if age >= 18:
# name
name = input("enter your firstname: ")
# ID
import random
x = "ID" + str(random.randint(101, 999))
print(f"your ID is: {x}")
print("you should memorize it")
y = input("enter password that has at list 8 caracterse: ")
# Password
while len(y) < 8:
y = input("enter password that has at list 8 caracterse: ")
print(f"your password is: {y}")
print("you should memorize it")
data = [x, y]
choice_1 = input("check your shopping cart(1): \nadd books to your shopping cart(2): ")
if choice_1 == "1":
self.__str__()
elif choice_1 == "2":
self.__iadd__()
# age < 18
elif age < 18:
print("this library is not for your age")
def __iadd__(self, other):
self.books.append(other)
return self
def __str__(self):
return f"customer {self.customer} bought those books{self.books}"
except:
pass
order = library(["the golsen company"], "Mr.asad")
print(order.books)
order += input("enter a book: ")
print(order.books)
OK, I have rewritten your code to implement it in a more organized way.好的,我已经重写了你的代码,以更有条理的方式实现它。 Your class "library" was not really a library at all;
您的 class “库”根本不是真正的库; it is a class for "orders", and I have renamed it as such.
它是“订单”的 class,我已将其重命名为。 I didn't know what you wanted for the manager account, so the manager account just assigns a fake user name without requiring a signup.
我不知道您对经理帐户的要求是什么,因此经理帐户只是分配了一个假用户名,而无需注册。 I also fixed the spelling errors and the tabbing.
我还修复了拼写错误和标签。
import random
import sys
class Order:
def __init__(self, books, customer):
self.books = books
self.customer = customer
def __iadd__(self, other):
self.books.append(other)
return self
def __isub__(self, other):
self.books.remove(other)
return self
def __str__(self):
return f"custumer {self.customer} bought those books {self.books}"
# sign in.
check = input("manager account(1),custumer account(2): ")
if check == '1':
x = 'manager'
if check == "2":
#age >= 18
age = int(input("enter your age: "))
if age < 18:
print("Sorry, you must be at least 18.")
sys.exit(0)
#name
name = input("enter your firstname: ")
# ID
x = "ID"+str(random.randint(101,999))
print(f"your ID is: {x}")
print("you should memorize it")
# Password
y = input("enter password that has at least 8 characters: ")
while len(y) < 8:
y = input("enter password that has at least 8 characters: ")
print(f"your password is: {y}")
print("you should memorize it")
# Main menu.
order = Order( [], x )
while True:
print('---')
choice_1 = input("1. check your shopping cart\n2. add books to your shopping cart\n3. remove books from your shopping cart\n4. quit: ")
if choice_1 == "1":
print( order )
elif choice_1 == "2":
order += input("enter a book: ")
elif choice_1 == "3":
book = input("enter a book: ")
if book in order.books:
order -= book
else:
print( f"{book} is not in your cart." )
elif choice_1 == "4":
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.