If Else not working within Method in Class

how to call a method inside if condition in java
if else statement example
if else statement java
how do if statements work
if else statement java exercises
if statement not working java
is valid for if statement
java if statement string

Code is terminating at line 11 after asking question. It does run if statement. Program is not giving any error either.

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()
  1. Make sure you save the user's input to an instance variable of the class and not just a local variable to the method.

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

  1. Print the "Have a good day" response by calling print.

  2. Never put a space between a function name and the open parenthesis.

  3. all variable names should be lower case and possibly containing underscores.

  4. Access the instance variable self.name inside the sorry() method, instead of just an null local variable name.

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

  1. Make sure you capitalize the response to "Do you still want it? Yes or No? : " and not the question itself.

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

  1. Make sure you save the user's name in the constructor, so that you can access it later.

self.name = name

Give this a try:

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()

defining method of class in if statement, Is there a better way to make objects with different methods? Yes a way that works :) Polymorphism is a great candidate. class Food: def� Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

You use upper function in wrong way,

correct way is this:

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

,You forget to put "Have a good day!" in print function

  print("Have a Good day!")

,also you lack in OOPS concept ,initialize

    self.name=name 

then you able to access name.

I rewrite your code and now it execute fine.Hope it help class 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()

ifelse, The if statement executes a statement if a specified condition is truthy. If the function testNum(a) {. 2 To execute multiple statements within a clause, use a block statement ( { . async function � block � break � class � const � continue � debugger � dowhile � empty � export � for � for awaitof � forin � forof� Hi sf ost, Just simply replace you test class with below class, Same code is working fine in my developer org. Class :- VerifyDate public class VerifyDate { //method to handle potential checks against two dates public static Date CheckDates(Date date1, Date date2) { //if date2 is within the next 30 days of date1, use date2.

Java If Else, Use the if statement to specify a block of Java code to be executed if a condition is true . It can be used to replace multiple lines of code with a single line. To check to make sure the "goals" array was being appended I did put a simple goal checking function in the Room parent class, and sure enough the game.goals gets appended. But my if-else statement doesn't seem to actually do anything; no matter what's in the goals array, I always get the else' desc when I come back to the room.

There a are a couple of problems:

1. Upppercase

In

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

you are applying upper() to the prompt text, not to the answer, given that in the following your code only checks for "YES" and "NO" you should use:

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

where the response is converted to uppercase instead.

2. "else not working"
    else:
        "Have a Good day!"

may be we forgot print here? :-)

Learning with Python 3, And if we get to the end of any function and we have not explicitly executed any return statement, Python automatically returns the value None. Tip: Understand� Understand self and __init__ method in python Class? self represents the instance of the class. By using the "self" keyword we can access the attributes and methods of the class in python. "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts. This method called when an object is created from the class and it allow the class to initialize

[PDF] If Statements and Booleans, The simplest if-statement has two parts – a boolean "test" within parentheses ( ) One useful sort of method for a class is one that tests if some condition is true� I had a weird issue: no matter what I couldn't access the public methods of one of my classes whether it was static or whether I setup to instantiate it. I ended up deleting the class and creating a new one with a different name. I must have been stepping on a built in class name.

4. More Control Flow Tools — Python 2.7.18 documentation, Perhaps the most well-known statement type is the if statement. This is commonly used for creating minimal classes: a value within a function (unless named in a global statement), although they may be referenced. Every class in java is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked.

If, If..else Statement in Java with Examples, If the condition is false then the statements inside if statement body are completely ignored. if statement flow diagram. Example of if statement. public class� Most services are not available until the Configure method is called. Multiple Startup. When the app defines separate Startup classes for different environments (for example, StartupDevelopment), the appropriate Startup class is selected at runtime. The class whose name suffix matches the current environment is prioritized.

Comments
  • input("Do you still...").upper()
  • Issue is not in your if else statement you wrongly accessing class name
  • On top of @Austin's comment.. The reason it isn't printing anything is there is no print statement in the else. It just makes a string "Have a Good day!" and does nothing with it.
  • You first upgrade Your oop concepts about class variable and instance variable access your instance variable by declaring it in init method
  • typecast your input Variable And then Upper and then compare