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
Related
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 1 year ago.
Improve this question
This code in under is will let the user enter the number of elements of list and the user need enter the elements ( User will need type number_of_elements_of_list times) and then count how many positive number, negative number, zero number. The code is like this:
the_number_of_elements_of_the_list = int(input())
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
if number_list == 0:
zero_number_count += 1
elif number_list == 1 or number_list > 1:
positive_number_count += 1
elif number_list < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
The code have a problem: The list can not compare like that. It will be error but i don't know how to compare the elements of list. Can you help me solve this problem?
Firstly, as teambob pointed out, add indent to the for-loop block.
Secondly, as DarkKnight pointed out, put the count variables outside the for-loop.
Thirdly, for each iteration, in order to use that value alone, use number_list[i-1] instead of number_list. (The index is i-1 rather than i because the range in your code starts from 1 rather than 0)
The final code would look like:
the_number_of_elements_of_the_list = int(input())
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
if number_list[i-1] == 0:
zero_number_count += 1
elif number_list[i-1] == 1 or number_list[i-1] > 1:
positive_number_count += 1
elif number_list[i-1] < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
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")
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.
This question already has answers here:
How to print X items in a list per line
(3 answers)
Closed 2 years ago.
i am really beginner and just starting programming with python since 2 weeks.
here is my question i cant find anywhere or i cant figure out the solution.
i want to see my result in terminal with multiple line , for example i want to calculate long for loop and the result show up this loop on terminal with very long a single line.
is there any solution for this?
and sorry for my bad english isnt my native language.
Code:
list = list(range(1,100))
for x in list:
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
Output:
3 5 6 9 10 12 15 18 20 21 24 25 etc...
But i want this one:
3 5 6 9 10
12 15 18 20 21
You could add an if statement before you print, to determine whether you want to have end be a blank or a newline character.
x_list = range(1,100)
n = 0
for x in x_list:
if x % 3 == 0 or x % 5 == 0:
if n == 4:
print(x, end='\n')
n = 0
else:
print(x, end=' ')
n = n + 1
You can change your print statement to:
print(x)
If you would print some results in the same line, you can do a logic to print some characters inline and after some iteration print in next line.
list1 = list(range(1,100))
for num, x in enumerate(list1):
if x % 3 == 0 or x % 5 == 0:
print(x, end=' ')
if num % 10 == 0:
print()
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 6 years ago.
Improve this question
Write a program that generates and prints a list of n elements (n informed by the user) containing the natural numbers (starting with 1) and replacing multiples of 3 by the word 'ping', multiples of 7 by the word 'pong', and multiples of 3 and 7 by the word 'ping-pong'
Here is the code for that
result = []
number = eval(input("Enter a whole number: "))
for index in range(number):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0:
result.append("ping")
elif index % 7 == 0:
result.append("pong")
else:
result.append(index)
print(result) == 0
Now also replaces numbers ending in 3 by the word ‘PING’ and numbers ending in 7 by the word ‘PONG’ this I am not sure how to go about doing.
I tried to make your code do what you want while doing as few modifications to it as possible.
Do NOT use eval. Ever. Bad, bad, bad eval. To cast an string to an int, use int().
Your code was starting at 0 when it was asked that it started at 1, I
changed the range.
To know the last digit, I calculated the number modulo 10, based on the clever comment by #Renuka Deshmukh. Other less clever solutions could have been to check the end of the number casted as a string, with str(index).endswith("7") or str(index)[-1] == "7", for example.
What was your print(result) == 0 trying to do? I removed the ==0.
Here is the resulting code:
result = []
number = int(input("Enter a whole number: "))
for index in range(1,number+1):
if index % 7 == 0 and index % 3 == 0:
result.append("ping-pong")
elif index % 3 == 0 or index % 10 == 3:
result.append("ping")
elif index % 7 == 0 or index % 10 == 7:
result.append("pong")
else:
result.append(index)
print(result)