How can i stop overlapping in this fizz buzz problem? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hello can someone help me clean up my code? how can i replace the numbers with the words fizz, buzz of fizzbuzz instead of overlapping. And also the program wont let me divide num by itself
problem:
Write a program that prints the numbers from 1 to 100.
But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
For numbers which are multiples of both three and five print "FizzBuzz".
Instead of only printing "fizz", "buzz", and "fizzbuzz", add a fourth print statement: "prime". You should print this whenever you encounter a number that is prime (divisible only by itself and one).
"""
#fizz buzz homework
for num in range(100):
print(num)
if num % 3 == 0:
print(num)
print("fizz")
if num % 5 == 0:
print(num)
print("buzz")
if num % 3 == 0 and num % 5 == 0:
print(num)
print("fizzbuzz")
# if num % num == 0:
# print(num)
# print("prime")

You got a few mistakes:
for num in range(100):
print(num) #This line is why all numbers are getting printed(you have no condition for them)
if num % 3 == 0:
print(num) # you dont want this line because if num % 3 == 0 you only want to print 'fizz'
print("fizz")
if num % 5 == 0:
print(num) # Same for this line you want to only print 'buzz'
print("buzz")
if num % 3 == 0 and num % 5 == 0: # Here you come to another problem beauce if num is dividable by 3 and by 5 then all 3 'if statements' will run. What you want to do is make sure that this is not the case before printing the others, you can do this by either changing your other 'if statements' to if num % 3 == 0 and num % 3 != 0 and vise versa
print(num) # same here you want to only print 'fizzbuzz'
print("fizzbuzz")
#if num % num == 0:
# print(num)
# print("prime")
Try this maybe:
for num in range(100):
if num % 3 == 0 and num % 5 != 0:
print("fizz")
if num % 5 == 0 and num % 3 != 0:
print("buzz")
if num % 3 == 0 and num % 5 == 0:
print("fizzbuzz")

Related

How do you make a running sum for fizz buzz in python?

So i am trying to make the fizz buzz program in python but that keeps a running sum of the numbers of fizz and buzz. so far i have this:
for number in range(100):
number += 1
if number % 5 == 0 and number % 3 == 0:
print('fizzbuzz')
elif number % 5 == 0:
print('buzz')
elif number % 3 == 0:
print('fizz')
else:
print(number)
You could have a sumFizzBuzz variable and accumulate to it as needed:
sumFizzBuzz = 0
for number in range(100):
if number % 5 == 0 and number % 3 == 0:
# Not sure if according to the requirements you sum fizzbuzz numbers too
print('fizzbuzz')
elif number % 5 == 0:
sumFizzBuzz += number
print('buzz')
elif number % 3 == 0:
sumFizzBuzz += number
print('fizz')
else:
print(number)

Python print different output if change order of if else [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I just a beginner in Python and today i see something strange with my if else output. I have following code:
for number in range(1, 100):
if(number % 3 == 0 and number % 5 == 0): ##this line now use first and output print correctly
print("FizzBuzz")
elif(number % 3 == 0):
print("Fizz")
elif(number % 5 == 0):
print("Buzz")
else:
print(number)
Above code print correctly, but if i change order of if else like this:
for number in range(1, 100):
if(number % 3 == 0):
print("Fizz")
elif(number % 5 == 0):
print("Buzz")
elif(number % 3 == 0 and number % 5 == 0): #this line not use first and output never have "fizzbuzz"
print("FizzBuzz")
else:
print(number)
Then the output will not print "Fizzbuzz" even loop until 15 or 30 or any number multiples of both 3 and 5
What going on, please help, thanks a lots
Because the the code start checking from the if part and then goes further elif parts if the previous one is false.
Basically, the number which is divisible by both 3 and 5 will meet the first if logic as it is divisible by 3 also in your second code, so it will execute that if and won't go further.
Here this code will execute like :
first it will check if(number%3 == 0), if it is true then it will print("Fizz") and rest of the elif and else will not be checked.
If 1 is not true then it will check elif(number%5 == 0)
Then by this way it will go down.
if(number % 3 == 0):
print("Fizz")
elif(number % 5 == 0):
print("Buzz")
elif(number % 3 == 0 and number % 5 == 0): #this line not use first and output never have "fizzbuzz"
print("FizzBuzz")
else:
print(number)
You have to test the most inclusive statement first. Let's walk through an an example for this code:
for number in range(1, 100):
if(number % 3 == 0):
print("Fizz")
elif(number % 5 == 0):
print("Buzz")
elif(number % 3 == 0 and number % 5 == 0): #this line not use first and output never have "fizzbuzz"
print("FizzBuzz")
else:
print(number)
number==3
if(number % 3 == 0): # 3 % 3 == 0, evaluates as True
print("Fizz")
The rest of your elif statements will not run. They only run if the if or elif statements above them return False
Same is true for all multiples of 3 and 5.
You have a single if statement with multiple conditions, which are checked from top to bottom. Once one condition is true, none of the rest are checked. If number % 3 == 0 and number % 5 == 0 would be true, then it is guaranteed that either number % 3 == 0 or number % 5 == 0 is true as well, so the FizzBuzz case is never reached.

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)

Why does calling the variable in this if statement give me a syntax error. What rule am I missing here? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am using python 3. I'm trying to understand why this code gives me a syntax error on my third line where it says "If not n % 2 == 0". I think there is some rule I am missing. What I'm trying to is impose some conditions on an inputted integer to give me a specific output.
n = int(input().strip())
def find_weird(n):
If not n % 2 == 0:
return "Weird"
Elif n % 2 == 0 and N < 5 and N > 2:
return "Not Weird"
Elif n % 2 == 0 and N < 20 and N > 6:
return "Weird"
Elif n % 2 == 0 and N > 20:
return "Not Weird"
Edit: Sorry, not properly copying the code snippet was my fault here (in the actual IDE I indented properly). It was the capitalization that got me at the end of the day. Sigh, what a way to post a first question on here. So embarrassing!
Python is case sensitive. if, elif and else should be in lowercase. Additionally, anything you want executed in such a block needs to be indented:
def find_weird(n):
if not n % 2 == 0:
return "Weird"
elif n % 2 == 0 and n < 5 and n > 2:
return "Not Weird"
elif n % 2 == 0 and n < 20 and n > 6:
return "Weird"
elif n % 2 == 0 and n > 20:
return "Not Weird"
if and elif are not capitalized. Python is case sensitive. Also, you need to indent your code. It should look like this:
n = int(input().strip())
def find_weird(n):
if not n % 2 == 0:
return "Weird"
elif n % 2 == 0 and n < 5 and n > 2:
return "Not Weird"
elif n % 2 == 0 and n < 20 and n> 6:
return "Weird"
elif n % 2 == 0 and n > 20:
return "Not Weird"
Three problems I see.
If and Elif should not be capitalized.
Use expr1 != expr2 rather than not expr1 == expr2
Indent your code properly after an if statement.
Try this code instead:
n = int(input().strip())
def find_weird(n):
if n % 2 != 0:
return "Weird"
elif n % 2 != 0 and n < 5 and n > 2:
return "Not Weird"
elif n % 2 == 0 and n < 20 and n > 6:
return "Weird"
elif n % 2 == 0 and n > 20:
return "Not Weird"

Prime Numbers python

I'm a beginning programmer in python and have a question about my code I'm writing:
number = int(input("Enter a random number: "))
for num in range(1, number + 1) :
for i in range(2, num) :
if (num % i) == 0 :
break
else :
print(num)
break
When I run this program I also get 9, 15 21 as output. But these are not prime numbers.
What is wrong with my code?
Thanks!
With if (num % i) == 0: you go to the else block for every num, which is not a multiply of 2, as you start i with 2, thus printing num. I got all odd numbers printed with your code.
You can use something like this:
number = int(input("Enter a random number: "))
for num in range(1, number + 1):
prime = True
for i in range(2, num):
if (num % i) == 0:
prime = False
break
if prime:
print(num)
It sets prime to False when it encounters a divisor without rest.
the trouble is in your else: statement which has to run outside of the first loop. I recommend the following:
def isprime(num):
for i in range(2, num):
if (num % i) == 0:
return False
return True
for num in range(1, number + 1) :
if isprime(num):
print num
Your problem
The else statement that you put inside the for loop means that if that number(num) is divisible by this current no represented by i, return true considering it as a prime which IS NOT CORRECT.
Suggestion
Your outer loop starts with 1 which should change to 2, as 1 is not a prime number
Solution
def fun(number):
#Not counting 1 in the sequence as its not prime
for num in range(2, number + 1) :
isPrime = True
for i in range(2, num) :
if (num % i) == 0 :
isPrime = False
break
if isPrime:
print num
fun(10)
Output
1
2
3
5
7
I am assuming the random number is the range you want the numbers to be within.
I found that the variable i is always equal to 2 in your code.This destroys the purpose of having a second for loop
Prime numbers are numbers that cannot be divisible by 2, 3 or 7, excluding 2, 3 or 7!
With this knowledge I adapted your code to show the true prime numbers.
solution
number = int(input("Enter a random number: "))
for num in range(2,number +1) :
printnum = True
if num == 2:
printnum = True
elif num == 3:
printnum = True
elif num == 7:
printnum = True
elif num % 2 == 0:
printnum = False
elif num % 3 == 0:
printnum = False
elif num % 7 == 0:
printnum = False
if printnum == True:
print(num)
This is my method to generate first ten prime numbers with no imports, just simple code with components we are learning on college. I save those numbers into list.
def main():
listaPrim = []
broj = 0
while len(listaPrim) != 10:
broj+= 1
brojac = 0
for i in range(1,100):
if broj % i == 0:
brojac += 1
if brojac == 2:
listaPrim.append(broj)
print(listaPrim)
if __name__=='__main__':
main()

Categories