How to use IF or ELSE statements in Python? BTW, How is my username and profile picture?

helpTHISoldMAN. will you

I want to write a function that takes number between 1 and 7 as a parameter and prints out the corresponding day as a string.

For example, if the parameter is 1, your function should print out one. If the parameter is 2, your function should print out two, etc.

I wrote this program, but I am not getting anything as an output. I sure that I am using the if and else statements correctly

My program:

def string(x):
  if x=="1":
      word = "one"
  else:
      if x=="2":
        word = "two"
    else:
        if x=="3":
            word = "three"
        else:
            if x=="4":
                word = "four"
            else:
                if x=="5":
                    word = "five"
                else:
                    if x=="6":
                        word = "six"
                    else:
                          if x=="7":
                              word = "seven"
                          else:
                              word = "Try again"
return word

def main():
  y = int(input("Please enter a number between 1 and 7: "))
  z = string(y)
  print(z)

main()
TigerhawkT3

All you have to do in this case is remove the int() call from the main(). Your string() function is expecting a string - if you send it an int, it'll never work. Additionally, you can use the elif keyword:

def string(x):
    if x=="1":
        word = "one"
    elif x=="2":
        word = "two"
    elif x=="3":
        word = "three"
    elif x=="4":
        word = "four"
    elif x=="5":
        word = "five"
    elif x=="6":
        word = "six"
    elif x=="7":
        word = "seven"
    else:
        word = "Try again"
    return word

def main():
  y = input("Please enter a number between 1 and 7: ")
  z = string(y)
  print(z)

main()

Or you can use a data structure called a dictionary:

def string(x):
    if x not in ('1', '2', '3', '4', '5', '6', '7'):
        return "Try again"

    d = {'1':'one', '2':'two', '3':'three', '4':'four',
    '5':'five', '6':'six', '7':'seven'}

    return d.get(x)

def main():
  print(string(input("Please enter a number between 1 and 7: ")))

main()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to use Array with if else statements?

From Dev

How to use if else statements in select form?

From Dev

How to use the if-else statements in batch programming?

From Dev

How to use values from if else statements

From Dev

How to get my Facebook Profile Picture as button image

From Dev

How to share a profile picture and get it in my chat app?

From Dev

How do I set my profile picture in Outlook for Mac 2011?

From Dev

How can I change my profile picture on Octopus?

From Dev

Laravel - how to get profile_picture from another table where the auth username matches?

From Dev

How to twitting in my profile use TwitterKit?

From Dev

How come my if else statements don't execute?

From Dev

How to remove my picture when I use my gmail account

From Dev

In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

From Dev

How to use multiple execvp calls with if-else statements?

From Dev

In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

From Dev

How to use if and else statements to achieve Age Classifier program

From Dev

How to debug my if and elif statements in Python?

From Dev

how to use 'else if' in my jQuery properly?

From Dev

How to refactor these if else statements to a function

From Dev

How to get python to recognize string value in DBF as part of if/else statements?

From Dev

How to Reduce Code Duplication of If-Else Statements in Python

From Dev

How to return User's profile picture that is stored in my filestyem (is /tmp folder save to put it there for temporary extraction)

From Dev

how to store gmail profile picture in session?

From Dev

How to get LinkedIn profile picture using OAuth?

From Dev

How to create circular facebook profile picture

From Dev

How to get twitter profile picture in ios?

From Dev

How to display Instagram Profile Picture with their API

From Dev

how to fetch profile picture from facebook

From Dev

How to get Facebook profile picture in android?

Related Related

  1. 1

    how to use Array with if else statements?

  2. 2

    How to use if else statements in select form?

  3. 3

    How to use the if-else statements in batch programming?

  4. 4

    How to use values from if else statements

  5. 5

    How to get my Facebook Profile Picture as button image

  6. 6

    How to share a profile picture and get it in my chat app?

  7. 7

    How do I set my profile picture in Outlook for Mac 2011?

  8. 8

    How can I change my profile picture on Octopus?

  9. 9

    Laravel - how to get profile_picture from another table where the auth username matches?

  10. 10

    How to twitting in my profile use TwitterKit?

  11. 11

    How come my if else statements don't execute?

  12. 12

    How to remove my picture when I use my gmail account

  13. 13

    In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

  14. 14

    How to use multiple execvp calls with if-else statements?

  15. 15

    In Corona SDK, how do I use the 'and' keyword in if, elseif, else statements?

  16. 16

    How to use if and else statements to achieve Age Classifier program

  17. 17

    How to debug my if and elif statements in Python?

  18. 18

    how to use 'else if' in my jQuery properly?

  19. 19

    How to refactor these if else statements to a function

  20. 20

    How to get python to recognize string value in DBF as part of if/else statements?

  21. 21

    How to Reduce Code Duplication of If-Else Statements in Python

  22. 22

    How to return User's profile picture that is stored in my filestyem (is /tmp folder save to put it there for temporary extraction)

  23. 23

    how to store gmail profile picture in session?

  24. 24

    How to get LinkedIn profile picture using OAuth?

  25. 25

    How to create circular facebook profile picture

  26. 26

    How to get twitter profile picture in ios?

  27. 27

    How to display Instagram Profile Picture with their API

  28. 28

    how to fetch profile picture from facebook

  29. 29

    How to get Facebook profile picture in android?

HotTag

Archive