How to use IF or ELSE statements in Python? BTW, How is my username and profile picture? -
How to use IF or ELSE statements in Python? BTW, How is my username and profile picture? -
i want write function takes number between 1 , 7 parameter , prints out corresponding day string.
for example, if parameter 1, function should print out one. if parameter 2, function should print out two, etc.
i wrote program, not getting output. sure using if , 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" homecoming word def main(): y = int(input("please come in number between 1 , 7: ")) z = string(y) print(z) main()
all have in case remove int() phone call main(). string() function expecting string - if send int, it'll never work. additionally, can utilize 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" homecoming word def main(): y = input("please come in number between 1 , 7: ") z = string(y) print(z) main() or can utilize info construction called dictionary:
def string(x): if x not in ('1', '2', '3', '4', '5', '6', '7'): homecoming "try again" d = {'1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven'} homecoming d.get(x) def main(): print(string(input("please come in number between 1 , 7: "))) main() python
Comments
Post a Comment