询问问题后,代码在第11行终止。它会运行if语句。程序也没有给出任何错误。
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()
答案 0 :(得分:0)
您使用上层功能的方式错误,
正确的方法是:
response = input("Do you still want it? Yes or No? :").upper()
,您忘记输入“祝您有美好的一天!”在打印功能中
print("Have a Good day!")
,您也缺乏OOPS概念,请初始化
self.name=name
然后您就可以访问名称。
我重写了您的代码,现在可以正常执行了。希望对您有所帮助 Car()类:
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()
答案 1 :(得分:0)
self.phone = input("Your phone number: ")
通过致电print
打印“祝你有美好的一天”响应。
请不要在函数名称和右括号之间放置空格。
所有变量名应为小写字母,并可能包含下划线。
在self.name
方法内访问实例变量sorry()
,而不仅仅是空的局部变量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()
答案 2 :(得分:0)
您的代码中有一些错误:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
// etc..
不会将用户的输入更改为大写,而是使您的提示字符串大写。因此,将其更改为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
答案 3 :(得分:0)
有两个问题:
在
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
转换为大写。
else:
"Have a Good day!"
也许我们在这里忘记了print
吗? :-)