Ask coding questions

← Back to all posts
I need help
MaryamNegeda (1)

Hello thx for wasting ur time pressing this and bothering to help :) .
Basically I'm making a program that can do certain well things
like option 1 option 2 option 3 etc
so, so far option 1 only works but 2 doesn't (I didn't make 3 or 4)

i would love if u guys can spare the time to help
ty

Answered by xxpertHacker (648) [earned 5 cycles]
View Answer
Commentshotnewtop
xxpertHacker (648)

I recommend creating a map of strings (options) to functions/lambdas (things that the option does).

If the option is not a map key, then show an error message.

If the key is present, get the map's value and call the function/lambda.

xxpertHacker (648)

@MaryamNegeda Seeing as you had accepted my answer, how well did it work out?

Example implementation:

def option_0():
	print("Option 0")

def option_1():
	print("Option 1")

options = {
	"0": option_0,
	"1": option_1
}

options[input("prompt: ")]()
RYANTADIPARTHI (3231)

Solution

I guess the way you are doing it will work, but the way I'm showing you is easier, and better, do it like this:

a = 1
a = int(a)
b = 2
b = int(b)
c = 3
c = int(c)
d = 4
d = int(d)

if a == 1:
   import turtle                      
   t = turtle.Pen()
   turtle.bgcolor("black")
   # and so on, write your code

so instead of <= or something, do == and turn the variables to ints, so this way is easier

That should work

Coder100 (12434)

You can't compare strings to numbers. So that means 1 >= "1" won't give you very defined behavior.
Consider making them an int:

a = int(input())

if a >= 1:
  ...
Shell101 (2)

I'm not 100% sure but since your variables have > or < sign it may get confused between which variable to pick? So I suggest just doing a = "1" b = "2". Idk hope that helped