0

I want to write this C++ code in Python:

cout<<"input number";
cin>>x;
switch(x)
{
 case '1':
   cout<<"my name is ali";
 case '2':
   cout<<"my name is hamza";
 default:
   cout<<"invalid input";
}
goto again:

I also checked the dictionary statement, but maybe I am coding it incorrectly.

Ali1331
  • 117
  • 2
  • 9

3 Answers3

1

This is one way to do it, there are no switch statements in python:

options = {"1":"my name is ali","2":"my name is hamza"} # map responses to keys

while True:
    x = raw_input("input number") # take user input, use `input()` for python 3
    if x in options: # if the key exists in the options dict
        print(options[x]) # print the appropriate response
        break # end the loop
    else:
        print("invalid input") # or else input is invalid, ask again
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Sir your technique is good... but your code is giving error on out put... can you please check it... and give me final form.... it will me more helpful please – Ali1331 Aug 09 '14 at 10:46
  • I would need to know the error, if using python 3 change `raw_input` to `input` – Padraic Cunningham Aug 09 '14 at 10:46
  • yes sir please check the error... i have uploaded snap shot.... >> http://prntscr.com/4b2onl >>> i am using python 2.7.5 not 3 – Ali1331 Aug 09 '14 at 10:58
  • Thanks sir,,,, i have also solved this >>> check final code >> options = {"1":"my name is ali","2":"my name is hamza"} # map responses to keys while True: x = raw_input("input number: ") # take user input, use `input()` for python 3 if x in options: # if the key exists in the options dict print options[x] # print the appropriate response break # end the loop else: print "invalid input" # or else input is invalid, ask again – Ali1331 Aug 09 '14 at 11:21
0

Python has no switch equivalent.

You can use if/else

if x == '1':
   print "my name is ali"
elif x == '2':
   print "my name is hamza"
else:
   print "invalid input"

If you are concerned that this may mean doing a lot of tests against x. You can often use a dict, with the keys being values of x. eg.

x_map = {"1": "my name is ali",
         "2": "my name is hamza"}

print x_map.get(x, "invalid input")
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

You can use dictionary like structure like this.

def my_switch(x):
    my_dict = {
            '1': 'my name is ali',
            '2': 'my name is hamza',
        }
    if x in my_dict.keys():
        return my_dict[x]
    else:
        return "invalid input"

Implementation of this function:

In [21]: my_switch('1')
Out[21]: 'my name is ali'

In [22]: my_switch('not a key')
Out[22]: 'invalid input'
salmanwahed
  • 9,450
  • 7
  • 32
  • 55
  • But sir i want to take input from user :(... I changed your code: def my_switch(x): my_dict = { '1': 'my name is ali', '2': 'my name is hamza', } if x in my_dict.keys(): print x return my_dict[x] else: return "invalid input" y=raw_input("enter number:") my_switch(y) ,,,,,,,,,,but it displays 1 instead of my name is ali... if i put digit 1 – Ali1331 Aug 09 '14 at 10:44
  • you can take input by, `x = raw_input()`. and I am not clear about the error you are getting. can you post your code? – salmanwahed Aug 09 '14 at 10:48
  • yes i took it with raw_input check >>> def my_switch(x): my_dict = { '1': 'my name is ali', '2': 'my name is hamza', } if x in my_dict.keys(): print x return my_dict[x] else: return "invalid input" y=raw_input("enter number:") my_switch(y) ,,,,,,,,,if i will enter 1 then it will display 1 not display 'my name is ali' ,,, i want to display value – Ali1331 Aug 09 '14 at 10:51
  • remove the `print` statement in `if` block – salmanwahed Aug 09 '14 at 10:53
  • i removed it also... but when user enter 1 then it should display... >> 'my name is ali' ......... ? so how to set it? – Ali1331 Aug 09 '14 at 11:00
  • are you running the code I have posted here? please post your code in your question. – salmanwahed Aug 09 '14 at 11:06
  • yes sir check this snap shot please.... http://prntscr.com/4b2r57 >> i am running your code... but i need to take input from user.... if user will press 1 then it should display 'my name is ali' .... that what i wanna do it – Ali1331 Aug 09 '14 at 11:11
  • 1
    Thanks Sir I have solved the problem now >> check that final code >>> def my_switch(x): my_dict = { '1': 'my name is ali', '2': 'my name is hamza', } if x in my_dict.keys(): return my_dict[x] else: return "invalid input" y=raw_input("enter number:") print my_switch(y) – Ali1331 Aug 09 '14 at 11:16