代码在提出问题后在第11行中终止。如果语句,它确实运行。程序也没有给出任何错误。
class Car():
def __init__ (self, name):
print (f"Hello and welcome {name}. You have won a Ferrari. Please enter the details below. n")
phone = input("Your phone number: ")
address = input("Your home address: ")
def sorry(self):
print ("Hey! We are sorry, we do not ship beyond 2000 Miles.")
response = input("Do you still want it? Yes or No? : ".upper())
if response == "YES":
print (f"Cool! {name}. Ride to us.")
elif response == "NO":
print (f"Oops! {name}. We are sorry to see you go. :(")
else:
"Have a Good day!"
Car1 = Car("Jack")
Car1.sorry()
- 确保将用户的输入保存到类的实例变量,而不仅仅是该方法的本地变量。
self.phone = input("Your phone number: ")
-
通过致电
print
。
打印"有美好的一天"响应 切勿在函数名称和开放括号之间放置空间。
所有变量名称都应较低,可能包含下划线。
访问
sorry()
方法内的实例变量self.name
,而不仅仅是null local变量name
。
print(f"Cool! {self.name}. Ride to us.")
- 确保您对"您仍然想要吗?是还是否?"而不是问题本身。
response = input("Do you still want it? Yes or No? : ").upper()
- 确保将用户的名称保存在构造函数中,以便以后可以访问。
self.name = name
尝试一下:
class Car():
def __init__ (self, name):
print(f"Hello and welcome {name}. You have won a Ferrari. Please enter the details below. n")
self.name = name
self.phone = input("Your phone number: ")
self.address = input("Your home address: ")
def sorry(self):
print("Hey! We are sorry, we do not ship beyond 2000 Miles.")
response = input("Do you still want it? Yes or No? : ").upper()
if response == "YES":
print(f"Cool! {self.name}. Ride to us.")
elif response == "NO":
print(f"Oops! {self.name}. We are sorry to see you go. :(")
else:
print("Have a Good day!")
ca1 = Car("Jack")
ca1.sorry()
您以错误的方式使用上层功能,
正确的方法是:
response = input("Do you still want it? Yes or No? :").upper()
,您忘记了"祝您有美好的一天!"在打印功能
print("Have a Good day!")
,您也缺乏OOPS概念,初始化
self.name=name
然后您可以访问名称。
我重写了您的代码,现在执行罚款。希望它有帮助 班级车():
def __init__ (self, name):
self.name=name
print (f"Hello and welcome {name}. You have won a Ferrari. Please enter the details below. n")
phone = input("Your phone number: ")
address = input("Your home address: ")
def sorry(self):
print ("Hey! We are sorry, we do not ship beyond 2000 Miles.")
response = input("Do you still want it? Yes or No? :").upper()
if response == "YES":
print (f"Cool! {self.name}. Ride to us.")
elif response == "NO":
print (f"Oops! {self.name}. We are sorry to see you go (: ")
else:
print("Have a Good day!")
Car1 = Car("Jack")
Car1.sorry()
您的代码中有一些错误:
-
response = input("Do you still want it? Yes or No? : ".upper())
不是将用户的输入更改为大写,而是使您的提示字符串大写。因此,将其更改为response = input("Do you still want it? Yes or No? : ")
.upper()
-
{name}
正在抛出错误,因为代码不知道名称是什么。因此,在您的课程中,将name
添加为字符串属性,在__init__
中定义了name
。稍后,使用占位符字符串将名称连接到打印字符串。 - 最后一个
else
语句缺少print()
修改的类Car
应该看起来像这样:
class Car():
name = ""
def __init__ (self, name):
self.name = name
print (f"Hello and welcome {name}. You have won a Ferrari. Please enter the details below. n")
phone = input("Your phone number: ")
address = input("Your home address: ")
def sorry(self):
print("Hey! We are sorry, we do not ship beyond 2000 Miles.")
response = input("Do you still want it? Yes or No? : ").upper()
if response == "YES":
print(f"Cool! %s. Ride to us." % self.name)
elif response == "NO":
print(f"Oops! %s. We are sorry to see you go. :(" % self.name)
else:
print("Have a Good day!")
有几个问题:
1。Upppercase
在
中response = input("Do you still want it? Yes or No? : ".upper())
您将upper()
应用于及时文本,而不是答案,给出在以下代码中,仅检查"YES"
和"NO"
您应该使用:
response = input("Do you still want it? Yes or No? : ").upper()
而response
转换为大写。
2。"不工作"
else:
"Have a Good day!"
可能是我们在这里忘记了print
吗?: - )