What's the error here?. I cant print the CozaLoza, CozaWoza and LozaWoza. And it only prints from 1-109. I also wants to print 11 output per lines. How can I do that?
for num in range (1, 22):
if (num % 3 == 0):
print("Coza" )
elif (num % 5 == 0):
print("Loza")
elif (num % 7 == 0):
print("Woza")
elif (num % 3 and num % 5 == 0):
print(" CozaLoza")
elif (num % 3 and num % 7 == 0):
print("CozaWoza")
elif (num % 5 and num % 7 == 0):
print("LozaWoza")
else :
print (num)
I updated the code as following:
num = int ( input ( "Enter a number: " ) )
if num < 1 or num > 110:
print("From 1-110 number is allowed")
else :
for n in range ( 1, num ) :
name = str(n)
if n % 3 == 0:
name = "Coza"
elif n % 5 == 0:
name = "Loza"
elif n % 7 == 0:
name = "Woza"
print(name, end=" ")
if (n + 1) % 10 == 0:
print("") # print new line each 10 number prints
The answer for your question is the use of if (n+1) % 10 == 0 and under.
It prints newline for each 10 numbers because of every mupltiple of 10 is True on (n+1) % 10 == 0.
BUT it has some wrongs on other lines.
NOT the for num in range(1, num), it is for n in range(1, num). the num is same with a name of variable in loops and it is ambiguous.
you could omit bracket ( and ) at if, elif statement in Python. if (A) equals if A in python :)
anyway, this is the output when input is 55:
Enter a number: 55
1 2 Coza 4 Loza Coza Woza 8 Coza
Loza 11 Coza 13 Woza Coza 16 17 Coza 19
Loza Coza 22 23 Coza Loza 26 Coza Woza 29
Coza 31 32 Coza 34 Loza Coza 37 38 Coza
Loza 41 Coza 43 44 Coza 46 47 Coza Woza
Loza Coza 52 53 Coza
EDITED: (by updated question)
when num is 10, the code can be reached to elif num % 3 and num % 5 == 0? I don't think so. because it was already True on elif num % 5 == 0 above.
you need to fix your if conditions as like:
if num % 3 == 0:
...
elif num % 5 == 0:
if num % 3:
# CozaLoza
else:
# Loza
Related
So, I have a homework. As you see below there're some cards below and each has a name. they're numbered from 0 to 35 and from left top to bottom. User enters a number and program has to tell the name, color and the number of the card. But there're some rules for it.
I can only use nested if and operators. Can't use while or other functions.
I can use max 13 if
can't use 36 if, else swap
this is the image of cards
I am not sure about what to do after this...
'''
num = int(input('Enter a number: '))
if num == 0 or num == 1 or num == 2 or num == 3:
print("6")
if num == 4 or num == 5 or num == 6 or num == 7:
print('7')
if num == 8 or num == 9 or num == 10 or num == 11:
print('8')
if num == 12 or num == 13 or num == 14 or num == 15:
print('9')
if num == 16 or num == 17 or num == 18 or num == 19:
print('10')
if num == 20 or num == 21 or num == 22 or num == 23:
print('vale')
if num == 24 or num == 25 or num == 26 or num == 27:
print('queen')
if num == 28 or num == 29 or num == 30 or num == 31:
print('king')
if num == 32 or num == 33 or num == 34 or num == 35:
print('tus')
'''
Here is what you are looking for in one line:
print(([str(i) for i in range(6, 11)] + ['vale', 'queen', 'king', 'tus'])[(int(input('Enter a number: '))) // 4])
The goal is to create a list of avalaible cards. Then use euclidien division by 4 to select the right card...
I'm just asking a quick question on how to format how numbers are presented in a text file with however many columns I want.
ie.)
1 3 4
2 6 10
3 18 6
As of right now my text file is just writing a new line for ever iteration and I'm having trouble figuring out how to get it to right a new line after 3 iterations of the calculation.
for Number in range(1, 10001):
count = 0
for i in range(2, (Number // 2 + 1)):
if Number % i == 0:
count = count + 1
break
if count == 0 and Number != 1:
file.write(str(Number) + "\n")
You could track the count of numbers written to file, and each multiple of three, write a newline.
output_count = 0
for Number in range(1, 10001):
count = 0
for i in range(2, (Number // 2 + 1)):
if Number % i == 0:
count = count + 1
break
if count == 0 and Number != 1:
file.write(f'{Number} ')
output_count += 1
if output_count % 3 == 0:
file.write("\n")
Code Refactor
It is recommended to refactor the code into functions that tackle pieces of the problem, such as calculating primes versus input/output. For example, in Python 3, you could write the following:
from typing import List
def generate_primes(maximum: int) -> List[int]:
primes = []
for n in range(1, maximum):
count = 0
for i in range(2, (n // 2 + 1)):
if n % i == 0:
count = count + 1
break
if count == 0 and n != 1:
primes.append(n)
return primes
def write_to_file(filename: str, data: List[int], columns: int) -> None:
with open(filename, 'w') as file:
output_count = 0
for n in data:
output_count += 1
if output_count % columns == 0:
file.write(f"{n}\n")
else:
file.write(f"{n} ")
if __name__ == "__main__":
data = generate_primes(10001)
write_to_file("primes.txt", data, columns=3)
Output
2 3 5
7 11 13
17 19 23
29 31 37
41 43 47
53 59 61
67 71 73
79 83 89
...
so i'm creating this function that gets a user to input a number greater than 2.
The code should then print all the prime numbers starting from 2 and ending at the number the user inputted.
So here is my code and it works (yay!)
def enterNumber():
number = int(input("Enter a number greater than 2"))
lower = 2
upper = number
for number in range (lower, upper):
if number > 2:
for i in range (2, number):
if (number % i) == 0:
break
else:
if (number % i) != 0:
print(number)
enterNumber()
BUT here is my output if the user inputs 18
"Enter a number greater than 2"
user puts "18"
output: 3
5
5
5
7
7
7
7
7
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
why is my output repeating itself? Any suggestions would be greatly appreciated thanks!
Your problem is here:
for i in range (2, number):
if (number % i) == 0:
break
else:
if (number % i) != 0:
print(number)
The else being inside the loop, says that you're going to print the number every time you find a non-divisor. For instance, for number = 11, you will print 11 for i values 2-10.
You don't know whether the number is a prime until you are done with the loop. Your print has to go after the loop, on a normal exit. An often-forgotten language feature is that a loop can have an else for exactly this purpose.
for i in range (2, number):
if (number % i) == 0:
break
else:
print(number)
Output:
Enter a number greater than 218
3
5
7
11
13
17
A simple solution(there are better ways):
def enterNumber():
number = int(input("Enter a number greater than 2"))
lower = 2
upper = number
for number in range(lower, upper):
prime = True
for i in range(2, number):
if (number % i) == 0:
prime = False
break
if prime:
print(number)
enterNumber()
please tell me how can I add line break after every 5 numbers
i = 1
while i < 30:
print(i, end = ' ')
i += 1
Use:
i = 1
while i < 30:
print(i, end = '\n' if i % 5 == 0 else " ")
i += 1
Should post an example for clarity, this should do it
for i in range(1, 30):
print(i, end=' ')
if not i % 5:
print('\n')
Just use the modulo operator % with an if-block to check if i is a multiple of 5:
i = 1
while i < 30:
print(i, end=' ')
if i % 5 == 0:
print()
i += 1
Output:
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
Try using the Modulo funktion, which is done with % in python.
i=1
while i<30:
print(i, end=' ')
if i%5 == 0:
print()
i += 1
the Modulo funktion returns the remainder of the division i/5, which is 0 for 0,5,10 and so on, so every 5 steps.
print('\n') prints a newline
This code is supposed to output fizz if number is divisible by 3, buzz if divisible by 5 and fizzbuzz if divisible by 3 and 5. Although I'm a bit unfamiliar with defining my own function and using the return appropriately. How do I remove the last 16 if the user inputs the number 16?
number = int(input("Enter a number: "))
def fizzbuzz(number):
n = 1
while n <= number:
if n % 3 != 0 and n % 5 != 0:
print(n)
elif n % 3 == 0 and n % 5 == 0:
print("fizzbuzz")
elif n % 3 == 0:
print("fizz")
elif n % 5 == 0:
print("buzz")
n = n + 1
return number
print(fizzbuzz(number))
If number = 16 it outputs
Enter a number: 16
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
16
How do I get I remove the last number 16, as it isn't supposed to be there
print("buzz")
n = n + 1
return number
print(fizzbuzz(number))
Here's your problem. Don't return the number, and don't print the return value of the function.
print("buzz")
n = n + 1
fizzbuzz(number)