Switch

Python

  1. def input():
  2. return int(raw_input())
  3.  
  4. number = input()
  5. if number == 8:
  6. print "Oxygen"
  7. elif number == 1:
  8. print "Hydrogen"
  9. elif number == 2:
  10. print "Helium"
  11. elif number == 11:
  12. print "Sodium"
  13. else:
  14. print "I have no idea what %d is" % number
  15.  
  16.  
  17. # Alternative solution
  18. number = input()
  19. db = {
  20. 1: "Hydrogen",
  21. 2: "Helium",
  22. 8: "Oxygen",
  23. 11: "Sodium",
  24. }
  25. print db.get(number, "I have no idea what %d is" % number)

Go

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strconv"
  6. )
  7.  
  8. func str2int(s string) int {
  9. i, err := strconv.Atoi(s)
  10. if err != nil {
  11. panic("Not a number")
  12. }
  13. return i
  14. }
  15.  
  16. func main() {
  17. var number_string string
  18. fmt.Scanln(&number_string)
  19. number := str2int(number_string)
  20.  
  21. switch number {
  22. case 8:
  23. fmt.Println("Oxygen")
  24. case 1:
  25. fmt.Println("Hydrogen")
  26. case 2:
  27. fmt.Println("Helium")
  28. case 11:
  29. fmt.Println("Sodium")
  30. default:
  31. fmt.Printf("I have no idea what %d is\n", number)
  32. }
  33.  
  34. // Alternative solution
  35.  
  36. fmt.Scanln(&number_string)
  37. db := map[int]string{
  38. 1: "Hydrogen",
  39. 2: "Helium",
  40. 8: "Oxygen",
  41. 11: "Sodium",
  42. }
  43. number = str2int(number_string)
  44. if name, exists := db[number]; exists {
  45. fmt.Println(name)
  46. } else {
  47. fmt.Printf("I have no idea what %d is\n", number)
  48. }
  49.  
  50. }