Jump to content

Need help with this beginner's code for Python.

LebowskiBuschemi

So I recently got into Python and I'm learning about Boolean logic. I was doing this exercise and I came upon this question that involved a customer getting one of three toppings. What's confusing me is how it's equal to 1 in Boolean logic if the customer's gotten only one topping. Is the 1 implying that since 0+0+1 is equal to 1, only one topping was put?

 

 

PythonLinus.JPG

Link to comment
Share on other sites

Link to post
Share on other sites

You've got it right, the == there is integer equality. It's converting the boolean variables to integers, specifically 0 for false and 1 for true, and adding those together. The sum (ketchup + mustard + onion) is equal to the total number of those variables that are true, so if the final value is one then only one variable is true, which is what that function is looking for.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

Yes. A boolean converted to int is 1 if true and 0 if false, so if you're comparing the sum with 1 that will only be true if there's one and only one that's true.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/3/2021 at 9:37 AM, LebowskiBuschemi said:

So I recently got into Python and I'm learning about Boolean logic. I was doing this exercise and I came upon this question that involved a customer getting one of three toppings. What's confusing me is how it's equal to 1 in Boolean logic if the customer's gotten only one topping. Is the 1 implying that since 0+0+1 is equal to 1, only one topping was put?

 

 

PythonLinus.JPG

This is a confusing problem. I've never seen anyone implicitly cast a boolean to an int to make the function shorter. Doing

exactly_one_topping(ketchup, mustard, onion)

, the arguments are positional. This is not very Pythonic. What if you wanted to add lettuce or cheese? You can see how the arguments can add up pretty quickly! Also, it could get difficult to maintain. 

 

The more pythonic way to do this would probably be 

def exactly_one_topping(**toppings):
  """Returns true if there is exactly one topping. 
  """
  has_topping = False  # No toppings
  for _, val in toppings:  # _ is the standard convention for skipping a value in a tuple
    if val: 
      if has_topping:
        return False  # Too many toppings
      has_topping = True  # One topping
  
  return has_topping

I'm not sure where these examples are coming from. Where are they?

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, noitcelfeR said:

the arguments are positional. This is not very Pythonic. What if you wanted to add lettuce or cheese? You can see how the arguments can add up pretty quickly! Also, it could get difficult to maintain. 

 

I'm not sure where these examples are coming from. Where are they?

The OP mentioned that the point was to show how booleans and their conversions worked, not to make "clean" code...

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, noitcelfeR said:

I've never seen anyone implicitly cast a boolean to an int to make the function shorter.

I use it all the time. Not specifically to make the function shorter by those precious 3 characters, but it's just nice and convenient that 0, empty lists, tuples and dictionaries, etc. all evaluate to False or 0. Makes for easy "if not X: <bla>" checks and some easy counting.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×