0

Code gives no output, can someone help?

import random tw=input() way1=["Left", "Right"] way2=["Left", "Right"] way3=["Left", "Right"] random.shuffle(way1) random.shuffle(way2) random.shuffle(way3) if tw=="Boston" or tw=="Washington": if tw=="Boston" and way1=="Left": print("Train is allowed to proceed through") break elif tw=="Boston" and way1=="Right": print("1st way, Switch switcher to the left") print("Train is allowed to proceed through") break elif tw=="Washington" and way1=="Right": print("Train is allowed to proceed through") break elif tw=="Washington" and way1=="Left": print("1st way, Switch switcher to the right") print("Train is allowed to proceed through") break Can't understand why programm gives no output, can someone help me understand what is my mistake? Also, thanks

16th May 2023, 11:37 AM
Anthony
Anthony - avatar
7 odpowiedzi
+ 5
Anthony Instead of copying and pasting the code, use Code Playground to share it. Just edit your question description and add a link to your code, using "+" button. This allows people to run and debug your code, and avoid copy errors.
17th May 2023, 11:08 AM
Emerson Prado
Emerson Prado - avatar
+ 4
Anthony break statement is used to stop the execution of loop when needed and is placed inside the loop another thing way1, way2, way3 are list so you can't compare them like way1 == "Left" it's always false instead use way1[0] == "Left"
16th May 2023, 11:44 AM
Just Some Random Guy
Just Some Random Guy - avatar
+ 4
Anthony There is no need for the breaks. Its an if-elif not a switch case. way1, way2, way3 are lists. after shuffling, if you want to get an item, you have to specify an index. Try this: import random tw=input() way1=["Left", "Right"] way2=["Left", "Right"] way3=["Left", "Right"] random.shuffle(way1) random.shuffle(way2) random.shuffle(way3) if tw=="Boston" or tw=="Washington": if tw=="Boston" and way1[0]=="Left": print("Train is allowed to proceed through") elif tw=="Boston" and way1[0]=="Right": print("1st way, Switch switcher to the left") print("Train is allowed to proceed through") elif tw=="Washington" and way1[0]=="Right": print("Train is allowed to proceed through") elif tw=="Washington" and way1[0]=="Left": print("1st way, Switch switcher to the right") print("Train is allowed to proceed through")
16th May 2023, 11:49 AM
Bob_Li
Bob_Li - avatar
+ 2
Thank you
16th May 2023, 11:45 AM
Anthony
Anthony - avatar
+ 2
Bob_Li nice one 👏, look he didn't use way2, way3, random.shuffle(way2) etc.. It's over code. A several repeats words in each string, so you can make var with that and use it. Don't repeat yourself.
17th May 2023, 3:26 AM
Smith Welder
Smith Welder - avatar
+ 2
Smith Welder probably more code left out. good idea on using variables for repeated messages. But I'm wondering if this is the best way to handle this? There's going to be a ton of branching possibilities that needs to be hardcoded... I would consider using a function to handle the branching decisions, and maybe dictionary for outcomes and actions. or OOP if the condtions are convoluted. Train switching is a graph structure.
17th May 2023, 3:43 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li My friend, of course, better way is save logic but make better algorithm. So as i told that make var and don't use way2.. 3 , a lots of if else etc.. So this code may looks like this: from random import randint w=["Left", "Right"] r1=randint(0,1);r2=randint(0,1) tr = "Train is allowed to proceed through" ns = "1st way, Switch switcher to the " def show(b,c=False): print (tr if c else ns+w[b]+"\n"+tr if b else ns+w[0]+"\n"+tr) show(r1,r2)
17th May 2023, 6:34 PM
Smith Welder
Smith Welder - avatar