FizzzBuzz Python Work Through - python

Count up from 1. If the number is a multiple of 7 they say "zap" instead of the number. If the number has a digit of 3 in it they say "buzz" instead of the number, and if both things are true they say "zap buzz".
Devise a function zap_buzz that plays a turn of the game. Given a positive integer parameter it should either return that integer value if neither of the "zap"/"buzz" conditions hold. If some condition holds, it should return the string "zap", the string "buzz", or the string "zap buzz", whichever is appropriate. You are allowed to assume the parameter is less than 1000. As in the earlier exercises, please don't convert the integer to a string to determine its digits.
>>> zap_buzz(8)
8
>>> zap_buzz(14)
'zap'
>>> zap_buzz(13)
'buzz'
>>> zap_buzz(35)
'zap buzz'
^^^^^ is the prompt. I have gotten so far:
def zapbuzz(x):
x =0
if x % 3 == 0 and x % 7 == 0:
return str("ZapBuzz")
elif x % == 3 and x % 7 != 0 :
return str("Zap")
elif x % 3 != 0 and x % 7 == 0:
return str("Buzz")
else /* x % 3 >= 1 or x % 7 >= 1:
return x
****Please don't give me the answer, but a few good tips or maybe a "try thinking about XYZ/etc/whatevertip in a different way would be super awesome. Thank you!
okay I read through the comments, thank you for inspo!
I made the following changes:
n = input()
def ZapBuzz(n):
if n%7 == 0 & n%3 == 0:
return ("ZapBuzz")
elif n%7 == 0 & n%3 != 0:
return ("Zap")
elif n%7 != 0 & n%3 == 0:
return ("Buzz")
else:
return (n)
okay, just talked to a tutor.... I worked out the function, except the issue im having now is that when i input 1 into the function, the terminal spits out 'none'.
def zap_buzz(x):
i = 0
t = False
while i < 3:
if ((x // 10**i)%10) == 3:
t = True
i += 1
if t == True and x % 7 == 0:
return "zap buzz"
if x % 7 == 0:
return "zap"
if t == True:
return "buzz"

Related

I'm trying to print the integers from 0 to 100 and identify the prime numbers

I've worked a lot on this but I can't figure out the problem. Can you help me?
The code should print numbers from 0 to 100 and print next to the prime numbers "Prime". The first function works but if I try to print the numbers all togheter the "prime" tag gets misplaced.
def testPrime(numTest):
if numTest <= 1:
return False
if numTest == 2:
return True
for i in range(2, numTest):
if (numTest % i) == 0:
return False
else:
return True
def printPrimes (lenght):
i = 0
while i <= lenght:
if testPrime(i):
print(str(i) + "Prime")
else:
print(str(i))
i += 1
printPrimes(100)
The logic in testPrime() is flawed. There are faster techniques but here's a trivial approach that may help:
def testPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5)+1, 2):
if n % i == 0:
return False
return True
Since a prime number is defined a number a natural number greater than 1 that is not a product of two smaller natural numbers
I think this is the only lines you need.
def testPrime(n):
for i in range(2,n):
if n%i == 0:
return False
return True

Python function to check if number is prime [duplicate]

This question already exists:
Checking if a number is prime in Python [duplicate]
Closed 2 years ago.
def is_prime(num):
lst = []
if num > 1:
pass
else:
return False
for number in range(0, 1000000+1):
if str(num) in str(number):
continue
elif str(1) in str(number):
continue
elif str(0) in str(number):
continue
lst.append(number)
for x in lst:
if num % num == 0 and num % 1 == 0 and not(num % x == 0):
return True
else:
return False
print(is_prime(9))
I do not know what is the problem with my code and I cannot find a solution the point of the program is to check whether the number is a prime number or not(prime number is only divisible by 1 and itself). The for loop just doesn't seem to work or something
def isprime(n):
return (all([False for i in range(2,n) if n % i == 0 ]) and not n < 2)
print (isprime(0))
print (isprime(1))
print (isprime(2))
print (isprime(3))
print (isprime(9))
print (isprime(10))
print (isprime(13))
Output:
False
False
True
True
False
False
True
Or:
def isprime(n):
if n < 2: return False
for i in range(2, n):
if n % i == 0:
return False
else:
return True
Line 16-19:
if num % num == 0 and num % 1 == 0 and not(num % x == 0):
return True
else:
return False
The first statement, num % num == 0 is finding the remainder when num is divided by num, which is always zero and therefore this statement is always true. num % 1 == 0 is also always true, so your code is equivalent to:
if not(num % x == 0):
return True
else:
return False
Which means, in the context of your program, "if the first value in lst is not a factor of num, then num is prime. Otherwise, num is not prime"
This only runs the loop one time and is backwards. Instead, you need to check if one of the numbers in lst is a factor to return False, and if all of the number is lst are not factors, then return True
for x in lst:
if num % x == 0:
return False
return True
for x in lst:
if num % num == 0 and num % 1 == 0 and not(num % x == 0):
return True
else:
return False
This loop can only ever run once, as it's guaranteed to return either true or false at the first iteration. If you find any factors, you want to return False. Otherwise, True.
for x in range(2, num):
if num % x == 0:
return False
return True
(Also, I'm not sure what was going on with the str shenanigans or the num % num / num % 1 conditions, both of which are always zero. But those should be unnecessary)
Except for 2 itself, you should only check divisibility by odd numbers up to the square root of the value.
Here's a short version:
def isPrime(N):
return all(N%p for p in range(3,int(N**0.5)+1,2)) if N>2 and N&1 else N==2

I am writing a program for a sample test at hackerRank but my loop is not working or i must say i am receiving only one output

Here is the code
def fizzBuzz(n):
n = list(range(1,n+1))
for numbers in n:
m3 = numbers / 3
m5 = numbers / 5
if type(m3) == int and type(m5) == int:
return "FizzBuzz"
elif type(m3) == int and type(m5) == float:
return "Fizz"
elif type(m3) == float and type(m5) == int:
return "Buzz"
else:
return numbers
challenge = fizzBuzz(5)
print(challenge)
I think the function should iterate over n times but I am getting only one output. Why is it so?
If you want to get all the output according to the list(range(1, n+1)) then use yield instead of return.
def fizzBuzz(n):
n = list(range(1, n+1))
for numbers in n:
m3 = numbers / 3
m5 = numbers / 5
if numbers % 3 == 0 and numbers % 5 == 0:
yield "FizzBuzz"
elif numbers % 3 == 0:
yield "Fizz"
elif numbers % 5 == 0:
yield "Buzz"
else:
yield numbers
challenge = list(fizzBuzz(5))
print(challenge)
Or you can initialize a list and append to the list then return
def fizzBuzz(n):
n = list(range(1, n + 1))
ls = []
for numbers in n:
m3 = numbers / 3
m5 = numbers / 5
if numbers % 3 == 0 and numbers % 5 == 0:
ls.append("FizzBuzz")
elif numbers % 3 == 0:
ls.append("Fizz")
elif numbers % 5 == 0:
ls.append("Buzz")
else:
ls.append(numbers)
return ls
challenge = fizzBuzz(5)
print(challenge)
Change your return statement to print statement or try this solution
def fizzBuzz(n):
for num in range(1, n+1):
if (num%3 == 0):
print('Fizz')
elif (num%5 == 0):
print('Buzz')
elif (num%3 == 0) and (num%5 == 0):
print('FizzBuzz')
else:
print(num)
challenge = fizzBuzz(5)
print(challenge)
When you divide any integer number by the '/' operator you always have the type float, even when division is an int. Test for yourself: type(2/2) returns float.
To check wheter N can be divided by M, it's usually better to test modulo N%M==0.
You can try this:
def fizz_buzz(n):
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)

Create a list of numbers dividable by certain numbers by using the command « filter() »

I’m on Python) I have to use filter() to create a list of all numbers from 1 to 100 (inclusive) that are dividable by 7, 9 and 42. I wrote this code, however, when I start it, it does not give me the right solutions. Do you know where the problem is ?
listnumbers = []
for x in range (1, 101):
x = str(x)
listnumbers.append(x)
print (listnumbers)
def dividable(k):
for t in k:
if int(t) % 7 == 0:
return True
if int(t) % 9 == 0:
return True
if int(t) % 42 == 0:
return True
else:
return False
return dividable
s2u = list(filter(dividable, listnumbers))
for q in s2u:
print(q)
According to your problem statement the number should be divisible by 7 and 9 and 42.
listnumbers = list(range(1,101))
def dividable(k):
if k % 7 == 0 and k % 9 == 0 and k % 42 == 0:
return True
else:
return False
ans = list(filter(dividable, listnumbers))
print(ans)
Just eyeballing your code, I think dividable(k) is not written correctly. Update to :
def dividable(k):
n = int(k)
return n % 7 == 0 or n % 9 == 0 or n % 42 == 0:
Why was there a for look declared in this method? And why was it returning the function at the end if it passed through the rest of the conditions?

Fizzbuzz Challenge in Twilio quest

I recently downloaded Twilio quest and I love it! However, I'm stuck at one of the entry-level Python challenges. It should be fairly easy to solve, but somehow I can not seem to work out the error here. Can anyone look through my code and find the obvious mistake which I clearly cannot?
import sys
inputs = sys.argv
inputs.pop(0)
for i in inputs:
print(i)
n = int(i)
for x in range(1,n+1):
if x % 3 == 0 and x % 5 == 0:
print("fizzbuzz")
elif x % 3 == 0:
print("fizz")
elif x % 5 == 0:
print("buzz")
else:
print(x)
The challenge is to solve the fizzbuzz challenge, with multiple numbers as input. Error yields:
"We passed your script a number that was divisible by both 3 and 5, and expected you to print fizzbuzz, but instead you printed -3000.
So the input was -3000, and should pass by my first test, as it is indeed divisible by both 3 and 5. I can not seem to work out why the input -3000 would jump to the "else"-part of my for-loop.
If the input is just a number, there is no need of the for loop.
The logic is correct.
One fix can be:
import sys
inputs = int(sys.argv[-1])
x=inputs
if x % 3 == 0 and x % 5 == 0:
print("fizzbuzz")
elif x % 3 == 0:
print("fizz")
elif x % 5 == 0:
print("buzz")
else:
print(x)
for a list of int in input:
import sys
inputs = [int(x) for x in sys.argv[1:]]
for x in inputs:
if x % 3 == 0 and x % 5 == 0:
print("fizzbuzz")
elif x % 3 == 0:
print("fizz")
elif x % 5 == 0:
print("buzz")
else:
print(x)
Does the question ask you to iterate over each number from 1 to N for every input or does it ask you to only check for a single Number for an input?
If it's only a single element then I believe the following code will suffice:
import sys
inputs = sys.argv
inputs.pop(0)
for i in inputs:
# print(i)
n = int(i)
if n % 3 == 0 and n % 5 == 0:
print("fizzbuzz")
elif n % 3 == 0:
print("fizz")
elif n % 5 == 0:
print("buzz")
else:
print(n)

Categories