0

I'm new to Python, and I'm trying to learn how to code simple String algorithms from my knowledge of Java. I'm trying to recreate this code segment in Python from Java:

Java:

import java.util.Scanner;
public class test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String[] list = {"Yes", "No", "Maybe", "So"};
        String userInput = input.nextLine();
        for(int i = 0; i < list.length; i++)
        {
            if(userInput.equals(list[i]))
            {
                System.out.println("oh baby");
            }
        }
    }
}

And here is my recreation in Python:

list = ["Yes", "No", "Maybe", "So"]
userInput = input()
for i in range(len(list)):
    if userInput is list[i]:
        print("oh baby")

but for some reason...in Python, it seems to not want to pass through the if statement.

  • 1
    Check [this](https://stackoverflow.com/questions/1504717/why-does-comparing-strings-using-either-or-is-sometimes-produce-a-differe). – chriptus13 Jun 16 '20 at 00:03

2 Answers2

2

Try this. Also check this.

list = ["Yes", "No", "Maybe", "So"]
userInput = input()
for i in range(len(list)):
    if userInput == list[i]:
        print("oh baby")
chriptus13
  • 705
  • 7
  • 20
0
test_list = ["Yes", "No", "Maybe", "So"]
user_input = input()
if user_input in test_list:
    print("oh baby")

This should work. Avoid using "list" as a variable as it is a function in Python. There is no need to iterate over the list as Python can use the keyword "in".

https://www.w3schools.com/python/ref_keyword_in.asp

Lem
  • 31
  • 4