-1

I am trying to call multiple functions in a conditional but is impossible :/

Here is my code:

print("""
    1. Something 1
    2. Something 2
    3. Something 3 """)
    
user = input("Choose some option: ")

def one():
    print("Something...")

def two():
    print("Something 2...")

def three():
    print("Something 3...")

if user == 1:
    one()
elif user == 2:
    two()
elif user == 3:
    three()

But I don´t know when I choose some option (1-3) never print the function...

aneroid
  • 12,983
  • 3
  • 36
  • 66
Drakarys
  • 41
  • 5
  • 1
    Please add the full traceback. – A random coder Jan 28 '21 at 13:19
  • It's not clear what's your problem. Do you have an error? – Yevhen Kuzmovych Jan 28 '21 at 13:22
  • 2
    You read strings, but compare against ints. `user = int(input("Choose some option: "))` to remedy. – user2390182 Jan 28 '21 at 13:23
  • Additionally, whenever you have multiple `if ... elif ... elif ...` blocks, always end with an `else` block to handle situations where none of the previous conditions are satisfied. Such as `else: print("input not recognized:", user)`. _(The only times it's okay to skip the final `else` is when (a) there's nothing to do with that situation and it's safe to continue or (b) you can guarantee that all the previous conditions have satisfied every possibility/potential values. And in the case of (b), your last `elif` should have been an `else`.)_ – aneroid Jan 28 '21 at 13:46

3 Answers3

0

I don't know where domain_ip comes from, but your current code doesn't work because the inputs are str and not int. Change the input line to

user = int(input("Choose some option: "))
Mitchell Olislagers
  • 1,758
  • 1
  • 4
  • 10
0

input() returns a string. You are checking against integers. That's why none of your conditions evaluates to true. Add a typeconversion before your if-block:

user = int(input("Choose some option: "))

and validate the input, or just check against strings inside your if-logic.

Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44
0

By default the input function parses what was given as a string. You need to cast it as int. If you change

user = input("Choose some option: ") # user here is a string

To:

user = int(input("Choose some option: ")) # now user is int

The code will work.