Repository navigation
Expand file tree
/
Copy pathfizzBuzz_lambda.py
More file actions
72 lines (51 loc) · 2.7 KB
/
Copy pathfizzBuzz_lambda.py
File metadata and controls
72 lines (51 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
'''
fizz buzz test with lambda functions
The fizz buzz tests is aimed to check if you know how to write a function.
In the fizz buzz test you are asked write a function that says 'fizz' when
a number is a multiple of x, and 'buzz' when it is a multiple of y.
In this case, the test uses lambda functions to evaluate the input integers.
# A lambda function is a quick way to evaluate an operation.
# A simple form can be: (lambda x: do something with x) (put x here)
For an example with regular functions, see fizzBuzz_normal.py
You may find useful documentation at the bottom.
© Damian Romero, 2019
Python 3.7
License: MIT license
'''
################################## Start program ##################################
# First, create a variable with input from user, aka standard input (stdin):
number = input("Tell me your integer and I\'ll fizz or buzz: ")
# Now, define the function which will evaluate the number in our variable:
def fizzbuzz(int:number): # int means the variable must be an integer
either = 0 # this switch will help us keep track of fizz and/or buzz
# We will use the modulo operator '%', which renders the reminder of a division.
if (lambda x: ((x%5)+(x%7)) == 0) (number): # if the sum of both modulos is 0:
print("fizz-buzz")
# If the sum is not 0, then maybe one of them is 0
else:
if (lambda x: (x%5) == 0) (number): # check if the reminder of number /5 is 0
print("fizz")
either += 1 # we have found a fizz, so add +1
if (lambda x: (x%7) == 0) (number):
print("buzz")
either += 1 # we have found a fizz, so add +1
# If we did not see a fizz or buzz, then tell the user:
if not either: # 'not' will evaluate 'true' if 'either' is empty or zero:
print("neither fizz nor buzz")
################################## Loop ##################################
# While loops keep happening as long as their condition is true:
while number.isnumeric(): # .isnumeric() is a string method to check for numbers
# we will convert the number to an integer
number = int(number)
# we will pass the number to our fizzbuzz function above
fizzbuzz(number)
# We will ask the user for another number.
# If the user types anything other than a number, the while loop will stop.
number = input("\nTell me your integer and I\'ll fizz or buzz or exit with enter: ")
################################## End ##################################
'''
Documentation
To learn more about modulo and other operations, go to: https://docs.python.org/3.3/reference/expressions.html
To learn more about lambda expressions, go to: https://docs.python.org/3/tutorial/controlflow.html
'''