Posts: 6
Threads: 2
Joined: Aug 2017
It seems if I try to type anything other than "Italy" or "italy", it still prints out ("I love that place, same here"), which supposed to print "not bad" considering the string that was plugged into the input wasn't Italy.
1 2 3 4 5 6 |
favoritePlace = input ( "how about your favorite place?" )
if favoritePlace is "italy" or "Italy" :
print ( "I love that place, same here" )
else :
print ( "not bad" )
|
Posts: 101
Threads: 7
Joined: Aug 2017
The python is operator matches the instance and not value.
To match value == is used.
1 |
if favoritePlace = = "italy" or favoritePlace = = "Italy" :
|
Posts: 6
Threads: 2
Joined: Aug 2017
Aug-31-2017, 02:22 AM
(This post was last modified: Aug-31-2017, 02:22 AM by Hussein26.)
(Aug-31-2017, 01:06 AM)hbknjr Wrote: The python is operator matches the instance and not value.
To match value == is used.
1 |
if favoritePlace = = "italy" or favoritePlace = = "Italy" :
|
I thought "==" is used only for numbers and "is" for strings. When do you use "is"
edit: I also tried "==" and it still never worked..whats the issue?
Posts: 4,220
Threads: 97
Joined: Sep 2016
You rarely use is. The is operator checks to see if both objects are stored in the same place (basically). It checks not just for a string 'italy', but a particular instance of the string 'italy'.
== checks to see if two things are equal (they have the same value). It doesn't care where they're stored.
As for == not working, did you notice the other problem with the or operator? Read this tutorial about the problem.
Posts: 6
Threads: 2
Joined: Aug 2017
(Aug-31-2017, 02:42 AM)ichabod801 Wrote: You rarely use is. The is operator checks to see if both objects are stored in the same place (basically). It checks not just for a string 'italy', but a particular instance of the string 'italy'.
== checks to see if two things are equal (they have the same value). It doesn't care where they're stored.
As for == not working, did you notice the other problem with the or operator? Read this tutorial about the problem.
I was completely wrong between "is" and "==" I spotted the difference and it had nothinggg to do between strings and numbers. I realized the code worked after I deleted the "or" off my statement. I read the tutorial, however, I never really understood it? I see I did the exact same mistake as his example of how to not use it. I was trying to put "or" to tell the computer to let it pass as true if they either put Italy or italy since there was no difference. I don't know what I have to do to let the computer know thats what I meant. The tutorial probably explained it, however, it tripped me out and it confused me.
Posts: 33
Threads: 2
Joined: Aug 2017
Aug-31-2017, 08:07 AM
(This post was last modified: Aug-31-2017, 08:07 AM by Sagar.)
I think the tutorial is trying to explain that:
1. or keyword expects a boolean expression after it.
2. if a string is placed after or, like what you have placed:
1 |
if favoritePlace is "italy" or "Italy" :
|
is always be evaluated as True
Anything or ed with True will get True as result
3. So instead if writing the conditional statement like above
You have to write like this:
1 |
if favoritePlace = = "italy" or favoritePlace = = "Italy" :
|
Posts: 1,298
Threads: 38
Joined: Sep 2016
Other solutions are by formatting the input and using lists. In the first example, we change the users response to all lower case (a common practice), so italy, Italy, iTaLy, etc would all evaluate to True. There are other options available, such as changing the input to all capital letters, but again, probably the most common is lower case.
The second way, with lists, checks if the users input exists in the list, if it does, it evaluates to True other wise False. You could combine the two methods so rather than having a list of all variants, you only need the lower case version. Asking for a favorite place, however, would result in a rather long list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
favoritePlace = input ( "how about your favorite place (using 'lower')?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "not bad" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (using list)?" ).lower()
if favoritePlace in places:
print ( "I love that place, same here" )
else :
print ( "not in list" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (combining and adding)?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "I will add that to my list" )
places.append(favoritePlace)
print ( "New list = " , places)
|
Output: C:\Python36\python.exe C:/Python/Games/scratch.py
how about your favorite place (using 'lower')?Italy
I love that place, same here
how about your favorite place (using list)?italy
I love that place, same here
how about your favorite place (combining and adding)?england
I will add that to my list
New list = ['italy', 'Italy', 'england']
Process finished with exit code 0
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 2
Joined: Aug 2017
(Aug-31-2017, 12:10 PM)sparkz_alot Wrote: Other solutions are by formatting the input and using lists. In the first example, we change the users response to all lower case (a common practice), so italy, Italy, iTaLy, etc would all evaluate to True. There are other options available, such as changing the input to all capital letters, but again, probably the most common is lower case.
The second way, with lists, checks if the users input exists in the list, if it does, it evaluates to True other wise False. You could combine the two methods so rather than having a list of all variants, you only need the lower case version. Asking for a favorite place, however, would result in a rather long list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
favoritePlace = input ( "how about your favorite place (using 'lower')?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "not bad" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (using list)?" ).lower()
if favoritePlace in places:
print ( "I love that place, same here" )
else :
print ( "not in list" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (combining and adding)?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "I will add that to my list" )
places.append(favoritePlace)
print ( "New list = " , places)
|
Output: C:\Python36\python.exe C:/Python/Games/scratch.py
how about your favorite place (using 'lower')?Italy
I love that place, same here
how about your favorite place (using list)?italy
I love that place, same here
how about your favorite place (combining and adding)?england
I will add that to my list
New list = ['italy', 'Italy', 'england']
Process finished with exit code 0
Wow, thats pretty sweet! Never looked at it like that. Thanks for much, opened my eyes about lists and adding .lower() to the end of my line. Thanks man!
Posts: 33
Threads: 2
Joined: Aug 2017
(Aug-31-2017, 12:10 PM)sparkz_alot Wrote: Other solutions are by formatting the input and using lists. In the first example, we change the users response to all lower case (a common practice), so italy, Italy, iTaLy, etc would all evaluate to True. There are other options available, such as changing the input to all capital letters, but again, probably the most common is lower case.
The second way, with lists, checks if the users input exists in the list, if it does, it evaluates to True other wise False. You could combine the two methods so rather than having a list of all variants, you only need the lower case version. Asking for a favorite place, however, would result in a rather long list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
favoritePlace = input ( "how about your favorite place (using 'lower')?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "not bad" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (using list)?" ).lower()
if favoritePlace in places:
print ( "I love that place, same here" )
else :
print ( "not in list" )
places = [ 'italy' , 'Italy' ]
favoritePlace = input ( "how about your favorite place (combining and adding)?" ).lower()
if favoritePlace = = "italy" :
print ( "I love that place, same here" )
else :
print ( "I will add that to my list" )
places.append(favoritePlace)
print ( "New list = " , places)
|
Output: C:\Python36\python.exe C:/Python/Games/scratch.py
how about your favorite place (using 'lower')?Italy
I love that place, same here
how about your favorite place (using list)?italy
I love that place, same here
how about your favorite place (combining and adding)?england
I will add that to my list
New list = ['italy', 'Italy', 'england']
Process finished with exit code 0
@ sparkz_alot that's a nice logic but I have a doubt over using a list (2nd one).
When you are converting the user input to lowercase, there is no point in storing "italy" and "Italy" in list before hand.
Posts: 12,025
Threads: 484
Joined: Sep 2016
I think He was showing an alternative for the comparison, and didn't
intend that both would be included in the final code
|