如果其他在类的Method中不起作用

时间:2019-03-09 06:13:32

标签: python python-3.x class

询问问题后,代码在第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()

4 个答案:

答案 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)

  1. 确保将用户输入保存到类的实例变量中,而不仅仅是方法的局部变量。

self.phone = input("Your phone number: ")

  1. 通过致电print打印“祝你有美好的一天”响应。

  2. 请不要在函数名称和右括号之间放置空格。

  3. 所有变量名应为小写字母,并可能包含下划线。

  4. self.name方法内访问实例变量sorry(),而不仅仅是空的局部变量name

print(f"Cool! {self.name}. Ride to us.")

  1. 请确保将大写的答复大写为“您是否仍要?是或否?:”,而不是问题本身。

response = input("Do you still want it? Yes or No? : ").upper()

  1. 确保将用户名保存在构造函数中,以便以后可以访问它。

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)

有两个问题:

1。大写字母

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吗? :-)