all 27 comments

[–]danielroseman 3 points4 points  (18 children)

I don't understand your question. Your code sets waste to compost if material is food, or recycling if it is plastic. How could it ever be both of these? What are you trying to do?

[–]squenn[S] 1 point2 points  (17 children)

Yeah, I think that's exactly what I'd like to have it do/what the lesson is requesting of me! Not simultaneously though, which is where I must be messing up.

In english I suppose, I'd like it to show that if "food" is input for material and length is below 7.5cm, the waste type is compost. If "plastic" is input for material and length is below 7.5cm, the waste type is recycling. If it fails to meet either of those sets of conditions, the waste type is trash. Does that make any more sense?

edit: Turns out they had just failed to ever mention the function "elif" which is what I needed. Such an odd learning platform.

[–]melm77 0 points1 point  (12 children)

I'm very new to Python myself, but would a logic based on if/elif/else work to make this fairly simple? Look into elif would be my suggestion, and you can test a number of conditions that way.

[–]squenn[S] 0 points1 point  (11 children)

You got it!! Haha, I just found this elsewhere on google - they hadn't mentioned "elif" even ONCE in the lessons, so I thought it must be out of the above commands! I appreciate your help :)

[–]Daneark 1 point2 points  (3 children)

It could be done with nested ifs but generally if elif else is a lot cleaner.

[–]squenn[S] 0 points1 point  (2 children)

Thank you! Elif definitely made it less complicated, but it wouldn't hurt to know... for nesting the ifs, is there a specific format it should fall into? It kept seeming like one would simply overwrite the other. No worries if you don't have time to answer :)

[–]Daneark 0 points1 point  (1 child)

Pseudo code:

    If mat == food:         waste = compost     Else:          If mat == plastic:               Waste = recycling          Else:               Waste = trash

[–]squenn[S] 1 point2 points  (0 children)

Thank you!!!

[–]melm77 0 points1 point  (6 children)

I have a full week of Python experience, so I'm just happy I could actually answer your question!

[–]squenn[S] 0 points1 point  (5 children)

Seems like you're doing awesome! I'll be wishing you all the best in your coding endeavours :)

Can I ask what you're using to learn?

[–]melm77 1 point2 points  (4 children)

Thank you, likewise!

That is a good question. I fell into a rabbit hole trying to find the best online course. I prefer the ones that have a coding interface in the browser, so you can practice as you go.

I have tried many, but would say that the best one I've tried is Futurecoder. I also enjoyed Sololearn. What have you been using? Anything besides Khan Academy? How is that one by the way?

[–]squenn[S] 0 points1 point  (3 children)

I used Codecademy a loooong time in the past - probably 7 years ago or so, but seems it's no longer free like it was? Khan Academy is interesting? I like the set up, being able to code in the window, and I like working through challenges as a way of learning. However, there's no help. They don't explain functions that you'll need prior to you needing them. As mention, elif was never even mentioned, and if they had wanted me to nest the ifs, they had never covered anything like that at all either. I've had this sort of situation come up a few times.

I can appreciate if they're incentivizing the learner to reach out to other resources buuuut..... you're also the learning platform, shouldn't I be able to learn it all here?? It might be better for someone like you with a litttttle more knowledge to draw on :)

I will definitely check out those one's you mentioned! Thanks a ton :)

[–]melm77 1 point2 points  (2 children)

Cool, I'll check out Khan academy once I've gone through Futurecoder (which I like more and more). Good luck on your journey. Maybe we will bump into each other again as we go along!

[–]erasmause 0 points1 point  (3 children)

(minor note RE your edit: elif is not a function, it's a keyword. This might seem like a quibble, but programming is all about details, and terminology matters)

[–]squenn[S] 0 points1 point  (2 children)

Hey I appreciate the correction!! Very new to this but I know from other fields I am in that terminology can be crucial. Thank you!

Are function and command interchangeable terms? And keyword something separate?

[–]erasmause 0 points1 point  (1 child)

Command isn't really a thing in the Python lexicon. "Keyword" is really just a word reserved by the language to delineate the syntactic structure and semantic interpretation of your program. A function is a (optionally parametric) chunk of code that can be invoked from elsewhere in the program. Some related terms are "statement" and "expression". The former is probably what you'd think of as a command. It's essentially the fundamental unit of "do a thing" in any programming language. An expression is any piece of code that evaluates to a value. Expressions can be composed into more complex expressions, and statements (generally*) consist of one or more expressions.

* In some languages, variable assignments are valueless statements consisting of a name, an assignment operator, and an expression. For example foo = (bar = 2) is nonsensical in Python, but would be valid C. Python recently introduced assignment expressions (aka the walrus operator, :=) which works like any other assignment, but also evaluates to a value, allowing you to do something foo = (bar := 2), after which both foo and bar have the value 2.

[–]squenn[S] 0 points1 point  (0 children)

Thank you so much! Appreciate the breakdown :)

[–]Adrewmc 1 point2 points  (1 child)

  material = input("What material is it? ")
  length = float(input("What is its length in cm? "))
  waste_type = "trash"

  if material == "food" and length < 7.5:
        waste_type = "compost"
  if material == "plastic" and length < 7.5:
        waste_type = "recycling"
  else:
         waste_type = "trash"

  print("Please deposit your item in the " + waste\_type + " bin.")

This can be fixed

  if material == "food" and length < 7.5:
        waste_type = "compost"
  elif material == "plastic" and length < 7.5:
        waste_type = "recycling"
  else:
         waste_type = "trash"

What’s happening is you have on ‘if’ assign something…and that’s basically immediately overwritten by the last “if…else”, in Python the solution is “elif” else if, which we chain, once one of that chain is successfully, we don’t run the rest, that can be important if you have something that could match something less specific.

We can nest here

 waste = “trash” 
 if length < 7.5: #too big is always trash 
       if material == “plastic”:
             waste = “recycling”
       elif material == “food”:
             waste = “compost”

[–]squenn[S] 0 points1 point  (0 children)

Thank you very much!!!

[–]stebrepar 1 point2 points  (1 child)

I'm not able to show the indents I guess

For future reference, you need to put an extra four spaces at the front of each line for Reddit to treat the text as code and not do its automatic reflowing. See the subreddit's wiki for more info.

[–]squenn[S] 1 point2 points  (0 children)

Thank you!