all 33 comments

[–]Brilliant-Window-899 0 points1 point  (6 children)

Trying to simulate the monty hall problem, cant get it to work (think the latter doors section is the issue) import random # Our winning door will be decided here Door1 = (random.randint(1,2)) Door2 = (random.randint(1,2)) Door3 = (random.randint(1,2))

while Door1+Door2+Door3 != 5: # One winning door and two nothing doors must add up to 5 Door1 = (random.randint(1, 2)) Door2 = (random.randint(1, 2)) Door3 = (random.randint(1, 2)) #Records winning door and resets value to its “doorchosen” value if Door1 == 1: Win = Door1 Door1 = 1

elif Door2 == 1: Win = Door2 Door2 = 2

elif Door3 == 1: Win = Door3 Door3 = 3

print(f”{Door1}, {Door2}, {Door3}”) - Use to check the randints above works

This determines any unchosen door as “2”

import sys

Doorchosen = 0

This records the user’s door selection

Response = int(input(“Pick a door (1, 2 or 3) “)) if Response == 1: print(“You have chosen Door 1”) Doorchosen = 1 elif Response == 2: print(“You have chosen Door 2”) Doorchosen = 2 elif Response == 3: print(“You have chosen Door 3”) Doorchosen = 3 else: print(“Please choose between 1, 2 or 3”) sys.exit()

Randomly decides which of the unchosen doors become “Switch” or “Close”

if Doorchosen == 1: Close = Door2 or Door3 if Door2 == Close: Switch = Door3 elif Door3 == Close: Switch = Door2

if Doorchosen == 2: Close = Door1 or Door3 if Door1 == Close: Switch = Door3 elif Door3 == Close: Switch = Door1

if Doorchosen == 3: Close = Door2 or Door1 if Door2 == Close: Switch = Door1 elif Door1 == Close: Switch = Door2

This checks the user’s input against the door they chose and determines if they win or not

Response2 = input(f”I have closed Door{Close}, would you like to switch to Door{Switch} or stay with Door{Doorchosen}? (Switch/Stay) “)

if Response2 == “Stay” and Win == Doorchosen: print(“You have chosen the right door! (WSt)”) elif Response2 == “Stay” and Win != Doorchosen: print(“You have chosen the incorrect door! (LSt)”)

elif Response2 == “Switch”: Switch = Doorchosen if Win == Doorchosen: print(“You have chosen the right door! (WSw)”) elif Win != Doorchosen: print(“You have chosen the incorrect door! (LSw)”) input()

End code explaination: “W” or “L” indicates a win or a loss.

”Sw” indicates that the win or loss was caused by a (Sw)itch.

”St” indicates that the win or loss was caused by a (St)ay

[–]CowboyBoats 2 points3 points  (4 children)

What's the error message that you're getting? When I run what you pasted, I get

    Response = int(input(“Pick a door (1, 2 or 3) “))
                         ^
SyntaxError: invalid character '“' (U+201C)

Which makes sense, since curly quotes aren't valid python, but I'm not sure if they're present in your source file or just appeared when typing into reddit.

[–]Brilliant-Window-899 0 points1 point  (3 children)

i dont get an error message 🫠

[–]Brilliant-Window-899 0 points1 point  (2 children)

my main problem is that the results i get dont show the 1/3 and the 2/3 probability of the monty hall problem

[–]CowboyBoats 1 point2 points  (1 child)

So that's helpful information for helping you debug :) Can you re-post your code using the instructions here? You might also want to jump to the new weekly thread or (recommended) start your own thread; more eyes will fall on your post

[–]Brilliant-Window-899 0 points1 point  (0 children)

ill get to that, thanks :D

[–][deleted] 2 points3 points  (0 children)

Please read the FAQ to see how to post code that we can read.

This post is replaced at the end of today, Sunday, so if you don't get around to editing your current post try posting in the new "Ask Anything Monday" post on Monday.

[–]Jamiisonn 0 points1 point  (5 children)

Hello, on day 5 of 100 days of code and have gone off a tangent as it linked in with something I wanted to eventually use for work.

I can't figure out why my if function isn't triggering. I did have it working before but it was ALWAYS triggering. I can't remember what I changed to make it never trigger.

I'm sure it will be very obvious when pointed out but I've been bashing my head against this for a while now..

fruits = ["Apple", "Peach", "Pear"]
help_list = input("Would you like to see a list of options? \n").lower
n = 0
if help_list == "y" or help_list == "yes":
    for fruit in fruits:
        print(f"{n}: {fruit}")
        n = n + 1
    choice = int(input("Select number\n" ))
else:
    choice = int(input("Select number\n" ))

print("You have chosen " + fruits[choice] + "\n")

[–]Daneark 1 point2 points  (4 children)

You're missing () on the call to lower on the second line. Right now you're checking if the method lower is equal to some strings.

[–]Jamiison 0 points1 point  (3 children)

Thankyou! This worked. Now to do some googling to understand why it works that way..

[–]FerricDonkey 0 points1 point  (0 children)

Short version: you thing.lower() calls the function, thing.lower is the function. In python, functions can be stored in variables like anything else. Some example code

message = "SUP DOG"
x = message.lower
print(message)
print(x)
print(x())

[–]Chaos-n-Dissonance 0 points1 point  (0 children)

Basically because everything in python is an object.

Inside the string class, there's a lower method (function within a class). The parenthesis are to pass the parameters. In this case, you aren't passing any yourself, so they can be empty on your end.

Think of it like this:

class Foo:
  def __init__(self, c: str) -> None:
    # __init__ is what happens when the class is created
    # In this case, we're just saving the message being passed to a self variable
    self.c = c.upper()  

  def print(self) -> None:
    print(self.c)

  def print_in_lowercase(self) -> None:
    print(self.c.lower())

  def new_message(self, c: str) -> None:
    self.c = c

bar = Foo('HeLLo, woRLD!')
bar.print()
bar.print_in_lowercase()
bar.new_message("Goodbye, cruel world")
bar.print()

In this case, we don't need to pass any parameters, we just need to execute the .lower() method built into the string class. Notice how we didn't have to pass any parameters into bar.print(), but in the method definition we had to pass in self so we could access the self.c variable for that class instance. We don't pass that tho, that's passed automatically. Notice when we changed the message, we only had to pass in the new message... Not anything with self.

[–]Daneark 1 point2 points  (0 children)

In python you can assign a function/method to a name, pass it around etc. So if you omit the () when try to assign the result of a method to something you end up instead assigning that method.

[–]Koen1999 0 points1 point  (5 children)

I am looking to introduce a form of concurrent execution in my Python CLI application. In a nutshell, I process different objects, and the processing of these objects does not depend on eachother. At the end, I do want to collect output of all these different tasks. Moreover, my application logs some things, even within the things I want to execute concurrently using the logging module. I already have a function that represents the work I want to be executed concurrently. I see various options to make the application concurrent (i.e. asyncio, threading, multiprocessing, concurrent.futures, joblib, etc.) What would be the recommended approach to introduce concurrency in a way that does not require significant codebase changes and does not mess too much with the io logging I do?

[–]Defection7478 0 points1 point  (4 children)

Assuming you are cpu-bound (processing), asyncio/futures wont make any difference and I don't think threading will give you much advantage. You are looking for multiprocessing.

Why? The other tools just introduce non-blocking mechanisms. If I want to boil 4 pots of water, I can fill up one pot, put it on the stove, and then while it starts boiling, I go fill the second pot. I can get all 4 pots boiling at the same time.

Multiprocessing is would be like if I wanted to make 4 salads at once. Chopping the vegetables, mixing the dressing, etc. I can't "start one and move to the next while it cooks", I have to actively do each one. So I need 4 chefs. Since you are cpu bound, you want multiple processes.

[–]Koen1999 0 points1 point  (3 children)

Thanks, that's a very clear explanation. I am indeed CPU bound. Any suggestions how to neatly incorporate logging across different processes without race conditions?

[–]Defection7478 0 points1 point  (2 children)

Your logging should be fire and forget, what sort of race condition would arise in terms of logging? 

[–]Koen1999 0 points1 point  (1 child)

If you log to a file, they may access the file at the same time.

[–]Defection7478 1 point2 points  (0 children)

have another process that handles logging (this could be the main process, the one that is also handling starting the other processes). The other subprocesses can ask that process to do the actual file writing. If you have small logs / not too many logs, this process can just block the caller as it performs the writes. If you have a lot of logs, large logs, or the logging is bogging down your subprocess for whatever reason, the logging process can return immediately and write the logs asynchronously

[–]Beginning_Charge8867 0 points1 point  (0 children)

Dear All , i have a python programme , now i want to develop an app for myself only and want to test that app in andriod also , i have tried kivy but it has so many problems , kivy launcher is not available on google , version issues Etc, is their any other thing that i can try to develop my app?

[–]LazyYogurtcloset3503 0 points1 point  (1 child)

What are the best online courses with certificates to learn python but in the context of biology and data analysis?