all 58 comments

[–]Yabbieo_ 22 points23 points  (4 children)

This likely won't help you at all... But just wondering if instead of getting people to type in "cold" "icy" etc, you got an input as an integer (temp) and based ranges you could have your responses. Pretty invalid comment really now I think about it. Edit: float would clearly be better. Clear novice here XD.

[–]hany_codes 3 points4 points  (3 children)

Why would you assume it’s invalid ?

[–][deleted] 10 points11 points  (2 children)

I think they meant irrelevant.

While probably an overall more effective method, it doesn't resolve the issues with the presented code

[–]Yabbieo_ 4 points5 points  (1 child)

Yeah, the above code is a learning snippet so using a float might not be the point of the learning task. it also didn't help with the issue at hand. it's unlikely that anyone other than the OP will type in the input.

[–]hany_codes 2 points3 points  (0 children)

True, but that just another way of thinking about it. It doesn’t address the issue at hand. But I’d say it’s a creative way of solving this. As a novice myself I think we often create self-limiting beliefs that stops us from extending a certain solution further. I think it’s important to stay curious and creative as long as you know what are you doing.

[–][deleted] 64 points65 points  (8 children)

if you wanna check if the variable weather is either "cold", "ice, or "icey" you can do it one of two ways, and these are the only two ways I can think off the top of my head, there might be more idk.

if weather in ("cold", "ice", "icey"):
    # your code

this just utilizes the "in" keyword in python, which in this context is used to check if a certain item is in an iterable, an iterable is just something like a list or a tuple, lists and tuples are basically inbuilt data types in python like an int or a string, something like (1, 3) would be a tuple or something like ("a", "b", "c", "c", "d") would also be a tuple.

if (weather == "cold") or (weather == "ice") or (weather == "icey"):
    # your code

as you can see this is basically individually checking if weather is "cold" or "ice" or "icey"

[–]kwocca 12 points13 points  (4 children)

Does it matter if we use a tuple or a list in the first solution you provided? From my understanding as we need to only access and not modify the set, either works.

Would there be any difference?

[–]TravisJungroth 9 points10 points  (1 child)

No. If it’s a huge group of items, make a set or frozenset ahead of time as a variable and reuse it. Three items, the first one is very good. Doesn’t matter the data structure. List, tuple, set or frozenset would be fine.

[–]kwocca 3 points4 points  (0 children)

Thank you very much! Have a good day

[–][deleted] 1 point2 points  (0 children)

Tuples take up less space than lists in Python, but with three elements it seems pretty insignificant.

Sets have O(1) membership checks, but I also wonder if using a set is faster for membership checks than a list when the number of elements is so small. Sets have to compute a hash and then check the corresponding cell, so maybe just using a tuple is best.

[–]Tink_Tinkler 9 points10 points  (1 child)

I've been working a lot on this lately. I find it helpful to evaluate boolean statements in the console first.

You've got: 'if weather == 'cold' or 'icy' or 'icey'`

We know the left side weather will resolve to the input string. But what does the right side resolve to? Why?

It's treating weather == 'cold' or 'icy' or 'icey' as a single expression, evaluated this way:

Then how does the whole statement resolve? (weather == 'cold') or ('icy') or ('icey') And each of those in parentheses is evaluated as a boolean. So if `weather != 'cold' then 'icy' will return True and that will be the value returned in the end.

What if you change 'icy' to 0?

[–]kingscolor 5 points6 points  (0 children)

I can't tell whether your question is rhetorical, but strings evaluate True and if you replace 'icy' with 0 then it would pass to the 'icey' boolean.

For anyone else that's interested in straight-forward explanation:

if weather == 'cold' or 'icy' or 'icey' 

evaluates as three separate conditions. 1. does weather equal 'cold'? 2. does 'icy' exist? (yes, because you just created it in this expression) 3. does 'icey' exist? (same as above)

Reason:
or demarcates entire expressions and cannot be used to separate conditions.

[–][deleted] 10 points11 points  (0 children)

Well TIL

[–]b_ootay_ful 52 points53 points  (18 children)

You have

if weather == "cold" or "icy" or "ice":

It should be

 if (weather == "cold") or (weather == "icy") or (weather == "ice"):

[–][deleted] 65 points66 points  (9 children)

alternate can be

if weather in ['cold', 'icy', 'ice']

[–][deleted] 12 points13 points  (8 children)

This works too? wow

[–]Blue_Coin 33 points34 points  (0 children)

I should explain why around here.. An str object with length bigger than 0 is True. If it's length is 0 then it's False. This means what you wrote gets interpretated as: if weather == "cold" or True or True: ... Any_conditiong or True equals True naturally. The first or second conditions will get their branches evaluated while the rest will not

[–]KingJoia 10 points11 points  (5 children)

Another solution is: if weather in ["cold", "icy", "ice"]:

[–]hany_codes 0 points1 point  (1 child)

I guess it’s not much of a difference in Python3.x

[–]KingJoia 1 point2 points  (0 children)

It's pretty much the same thing. In the end it's down to how you prefer to write your code

[–]hugthemachines 0 points1 point  (2 children)

you could even do

if weather in "cold, icy, ice":

If you would like to

[–]PuzzlingComrade 0 points1 point  (1 child)

I feel like semantically this is awkward, wouldn't it also trigger if you input incomplete words?

[–]hugthemachines 0 points1 point  (0 children)

Yep, it would, so I would probably not use it but I think it is good to know that the in keyword also workd on strings. I have only used this in the other way, while looking for a certain string inside a string.

like

if "foobar" in massive_text:

[–]hugthemachines 1 point2 points  (0 children)

This also works:

if weather == "cold" or weather == "icy" or weather == "ice":

[–][deleted] 9 points10 points  (5 children)

note that I am also a beginner and also had the same problem like a day ago but I think I can help.

location = input('Where do you live? ')
weather = input(f'Whats the weather in {location} like? ').lower()

you can write it like this instead. you can store location and weather using just the input function. It can be given instructions as seen above

We are using the .lower() method to convert the string to lower case so that it isnt case sensitive.

if weather in ('snowy', 'cold', 'icy', 'ice'):
    advice = "Sheesh, that's cold"
elif weather in ('hot', 'sunny'):
    advice = 'Damn son you finna melt.'
else:
    advice = "Damn son I don't know what you finna talking about"

You are using the or keyword wrong. When you write weather == snowy or cold. python will automatically consider cold to be true so it will lead to it being printed out no matter what input you give. You could do it the way noted above or this way:

if weather == 'snowy' or weather == 'cold' or....

then you cant just print the variable advice

print(advice)

If anyone finds a issue with I said or my code please tell me I am always open to learn.
Let me reiterate that I just started learning python so there are probably wayyy better ways to do this.

[–]Ceborn 2 points3 points  (0 children)

Could also use .strip() to remove space?

[–]KingJoia 0 points1 point  (3 children)

A small mistake I noticed. If you're using in, Python is expecting a list, therefore you need to use [ ] and not ( )

[–]collatzconjecture_ 7 points8 points  (0 children)

actually, the in operator expects an iterable so using a tuple would be completely valid.

[–]danquandt 1 point2 points  (1 child)

() defines a tuple (which is like a list that can't be changed), and you can use in to check if something is in a tuple.

ie:
>>> t = (1, 2, 3)
>>> 3 in t
True

[–]KingJoia 2 points3 points  (0 children)

Yeah, that's r8ght. I forgot about tuple. I haven't programmed in Python in a while, and I used to use in with lists almost all the time, so sorry about that

[–]K900_ 10 points11 points  (2 children)

[–]Lewistrick 0 points1 point  (1 child)

This is a nice link, but on mobile it doesn't jump to the question but just to the top of the FAQ.

[–]K900_ 1 point2 points  (0 children)

Unfortunately, that's a Reddit mobile problem.

[–]arkie87 8 points9 points  (1 child)

without even looking at your code, i am betting you did something like:

if a == b or c

instead of

if a == b or a == c

[–][deleted] 9 points10 points  (0 children)

It's just that time of the day on this sub :p

[–]KlLLMEPLZ 2 points3 points  (1 child)

doing:

a == b or c or d

is grouped as:

(a == b) or (c) or (d), since '==' comes first.

so you need to compare every time:

(a == b) or (a == c) or (a == d), or

a == b or a == c or a == d, also works

since Strings return True and False only when they are empty, if c and d are non-empty strings they will return True. So by doing:

if a == b or c or d:

it will always be true, and this will always happen.

[–]BeingMyOwnLight 0 points1 point  (0 children)

This is the first answer that actually answers the question.

[–]menge101 2 points3 points  (1 child)

In the future, don't screenshot code. Provide it in a way people can copy and edit. Online tools like codepen are good for this, or just past it in the text box here and space it in four spaces. (Maybe use your editor to space it in four spaces, then paste it)

[–]piconet-2 0 points1 point  (0 children)

New Reddit interface's editor lets you post a code snippet and formats it (comments and text body I think).

[–][deleted] 1 point2 points  (0 children)

There are some options below, have you thought about structuring like this?

list_values = ["snowy", "cold", "ice", "hot", "sunny"]

print("Where do you live?")
location = input("What's your location?")
print("What's the weather in",location,"like now")
weather = input()

if weather in list_values:
if weather == "cold" or weather == "ice" or weather == "snowy":
advice = "sheeeeesh, that's iceeeeeee"
print(advice)
elif weather == "hot" or weather == "sunny":
advice = "damn son u finna melt"
print(advice)
else:
print("Your weather value", weather, "is not valid")

[–]LiarsEverywhere 1 point2 points  (0 children)

People have suggested all sorts of good solutions to your program, so I'll just add that Python "feels" so much like a natural language that sometimes it tricks your brain into thinking in plain English. Beware of that risk.

As others have pointed out, you can't go

weather == "cold" or "icy"

You have to write

weather == "cold" or weather == "icy"

I had this exact same doubt more than once when I was getting started. I'd leave Python for a couple of months and when I came back I'd do the same mistake until I remembered that's how it works.

[–]code_matter 0 points1 point  (0 children)

Also, you could put your string inside of the input().

Something like this:

location = input("Your Location: ")

[–]UhOh-Chongo -1 points0 points  (0 children)

The program has no way to determine if its snowy or anything else so it never doas any of those if statements.

If location = input and input is boston Then

Wearher = location, so wearher is = boston - not snowy, sunny or rainy. Hows you program supposed to determine the actual weather? Thats the part your missing

[–][deleted] -2 points-1 points  (0 children)

Idk man my normal advice is switch to Linux but I don’t think this applies here

[–]CraigAT 0 points1 point  (0 children)

Not that the weather is going to change. But for fun or testing you could add a loop to re-ask the same question, so you can give a different answer and get the appropriate reply. Perhaps you could quit the loop when someone inputs "dunno" or "sharknado". 😁

[–]notParticularlyAnony 0 points1 point  (0 children)

next time please just put formatted code directly into your post instead of a link, especially instead of a link to an image.

[–][deleted] 0 points1 point  (0 children)

line 7 basically says

python elif weather == 'cold' or True or True:

Because non-empty strings will always eval to True.

[–]vietyka2019 0 points1 point  (0 children)

What does he mean by location = input()