Python print different output if change order of if else [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
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.

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)

How can i stop overlapping in this fizz buzz problem? [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
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")

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)

Find number based on modulo conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to upgrade my code and I need to add function to find a number divided by 7, and getting modulo from 2,3,4,5,6 = 1.
I wrote that code:
a = 0
while a != 1:
x = 0
if(x % 2 == 1 and x % 3 == 1 and x % 4 == 1 and x % 5 == 1 and x % 6 == 1
and x % 7 == 0) == True:
a = 1
print(x)
else:
a = 1
x = x+1
I need to find first number divided by 7 and get all modulo from 2 to 6 ==1
Your loop is a while loop while a != 1.
However, regardless of what happens (your number x meets the criteria or not), a is immediately set to 1, meaning that after only 1 run, your loop will immediately end.
Furthermore, since x=0 at the start of any run of the loop, x will never meet your criteria.
Try re-designing your algorithm, because the way you're getting your number doesn't work at all.
Personally, I'd suggest using a while True, and you don't need a, only x.
Here's what I wrote
x = 0
while True:
if(x % 2 == 1 and x % 3 == 1 and x % 4 == 1 and x % 5 == 1 and x % 6 == 1 and x % 7 == 0):
print(x)
break
x += 1
The first number I got was 301.
You could loop through multiples of 7 and check the condition while doing so, you can use all() to check for those conditions as the following:
i = 7
while True:
if all(i%x==1 for x in range(2, 7)):
print(x)
break
i += 7

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"

Categories