is there a way to make the x odd - python

I have been trying to make a simple code with python in which it takes an input 'n' and outputs the numbers from 1 to n
for each multiple of 3, print Solo instead of the number
for each multiple of 5, print Learn instead of the number
for numbers which are multiples of both 3 and 5, output SoloLearn
I want to code it in a way that the logic will skip the even numbers and can only be applied to odd numbers in the range
this is my code below
n = int(input())
for x in range(1, n):
if x % 3 == 0 and x % 5 == 0:
print("SoloLearn")
elif x % 3 == 0:
print("Solo")
elif x % 5 == 0:
print("Learn")
else:
print(x)

Add a continue clause at the top of your loop:
if x % 2 == 0:
continue
For more details, read the docs.

The simplest/fastest/smallest amount of changes you need to make is to just alter the range function to give you exactly what you want.
n = int(input())
for x in range(1, n + 1 , 2):
if x % 3 == 0 and x % 5 == 0:
print("SoloLearn")
elif x % 3 == 0:
print("Solo")
elif x % 5 == 0:
print("Learn")
else:
print(x)
I added n+1 not to satisfy your question, but because I think as written your code was not correct -> for example run these two snippets and see what I mean. If you didn't want n to be part of the range than your code is correct without the n+1.
print("no +1")
for i in range(1,11,2):
print(i)
print("\n\nwith + 1"
for i in range(1,11+1,1):
print(i)

Convert the 'for' loop to 'while' loop . Since we are starting with 1 , adding 2 in each iteration , makes the loop run only for odd numbers.
n = int(input())
x=1
while x<n:
if x % 3 == 0 and x % 5 == 0:
print(x ,"SoloLearn")
elif x % 3 == 0:
print(x, "Solo")
elif x % 5 == 0:
print(x, "Learn")
else:
print(x)
x=x+2

Related

How to print only once in loop?

b = input().split(' ')
c = list(map(int,b))
y = 0
for i in range(len(b)):
if c[i] %2 == 0:
print(c[i])
y = 1
elif i == len(c) - 1 & y == 0:
print('No number that is divisible by 2')
Code takes a list of numbers as input, then it prints all values divisible by 2, but if there are no such values it should print about this only once. My code works properly only in certain cases. I know another implementation but i need solution within 1 loop
Add break:
b = input().split(' ')
c = list(map(int,b))
y = 0
for i in range(len(b)):
if c[i] %2 == 0:
print(c[i])
y = 1
elif i == len(c) - 1 & y == 0:
print('No number that is divisible by 2')
break
You may want to simplify by calculating the numbers divisible by 2 first, then deciding what to print:
nums = map(int, input().split(' '))
div2 = [i for i in nums if not i % 2]
if div2:
for i in div2:
print(i)
else:
print('No number that is divisible by 2')

"super" number in python

I have a problem with the task. The task is:
We say that number 1 is a super number. If a number x is super, then
the numbers 2x and 3x are also super. For example, since the number 1
is super, then the numbers 2 and 3 are super. As 2 and 3 are super,
then the numbers 4, 6 and 9 are super, and so on. At the same time,
numbers 10 and 7 are not super. Write a program that asks the user to
enter a natural number n. The program prints whether the entered
number is super.
And this is what I have done so far
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 5 == 0 or num % 7 == 0 or num % 11 == 0 or num % 13 == 0:
print("Number is not super.")
elif num == 1 or num % 2 == 0 or num % 3 == 0 or num % 8 == 0 or num % 9 == 0:
print("Number is super")
else:
print("Number is not super.")
The problem is that for some numbers like 62 it says that it is a super number, but it ain't..
Just following the definition of super number:
def is_super(k):
if k == 1: return True
if k % 2 == 0:
return is_super(k / 2)
if k % 3 == 0:
return is_super(k / 3)
return False
Some testing
print(is_super(9))
True
print(is_super(14))
False
print(is_super(32))
True
print(is_super(62))
False
To me it looks like you jumped in without first figuring out how you'd work it out manually.
Personally, I think the easiest way to start this would be recursively, though it'll be very inefficient with large numbers, so it's up to you if you then want to go and optimise it after doing the first version. I got it working pretty easily, but since it's a problem you need to solve, I'm not going to just copy and paste the answer.
It works something like this (psuedocode):
def is_super(i):
if i is below 1: not a super
if i is 1: is a super
if i is above 1: check if i/2 or i/3 is a super
There are some nice recursive solutions here, I'll propose a non-recursive one. As some of the comments hint, super numbers have a tell-tale factorization: they are all of the form 2x * 3y for x, y >= 0, and every integer of that form is a super number. This should become clear after some study because the only way to get a super number is to multiply an existing one by 2 or 3, and we start with 1. That leads to the following code to test for superness:
def is_super(n: int) -> bool:
if n < 1:
return False
while n % 3 == 0:
n //= 3
return n & (n - 1) == 0; // Suggested by #KellyBundy
I tried to find shortcut way to test for superness but failed to find anything better than this.
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 3 == 0:
while num % 3 == 0:
num = num / 3
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
elif num % 2 == 0:
while num % 2 == 0:
num = num / 2
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
else:
print("Number is not super")
This is what I've done so far but I don't think it will work for large numbers ?

Python code for finding the 1001st prime number

I recently started programming Python and was stuck at this particular Project Euler problem for finding the 1001st prime number.
My code:
L = [2]
def is_prime(x,L):
for el in L:
if x%el != 0:
flag = 1
else:
flag = 0
break
if flag == 1:
return True
elif flag == 0:
return False
a = 3
for k in range(1,1002):
if is_prime(a,L) is True:
L.append(a)
a += 1
k += 1
else:
a += 1
print(L)
print(len(L))
This prints the list of prime numbers upto 997 which is the 168th prime number. Can you tell me what am I doing wrong?
Your increment of k is pointless, because you're iterating a fixed range of numbers (k is replaced when the loop loops, the range can't be changed dynamically).
You want a while loop with a manually managed k:
a = 3
k = 1
while k < 1002:
if is_prime(a,L):
L.append(a)
a += 1
k += 1
else:
a += 1
Note: There may be some off-by-one logic errors here, but it expresses what I assume was your intent in the original code, fixing only the issue with managing k.
k += 1
That's where the problem is. Try this code:
for k in range(1, 1002):
print(k)
and then:
for k in range(1, 1002):
k += 1
print(k)
Your Mistake is that you are finding numbers that are prime UP to 1001 and Also variables of a loop cannot increase By the user
to fix that use a while loop
A Program To Find Up Prime's To Any Number would be:
import sys
def prime_num_finder():
i = 2
while True:
try:
p = int(input("Please enter up to what number the prime numbers should be: "))
break
except ValueError:
sys.stderr.write('ERROR\n')
sys.stderr.write(' Try again...\n')
while i != (p + 1):
x = i
if x % 2 and x % 3 and x % 5 and x % 7 > 0.1: # Checks if the remainder is greater than 0.1 to see if it has an remainder
r1 = True
else:
if i == 2 or i == 3 or i == 5 or i == 7:
r1 = True
else:
r1 = False
if r1 == True:
print(i, " is a prime number!")
else:
pass
i = i + 1
prime_num_finder()
quit()
That is a simple program to check if it is divisible by 2 ,3, 5, or 7

Divisible by two given integers

x=[1,2,3,4,5,6]
for y in x:
if y %2 == 0:
print (y)
elif y %3 == 0:
print ("y")
elif y %3 and y %2 ==0:
print ("Divisible by 2 and 3!")
else:
print ("Odd number!")
I was trying to find out the even and odd numbers between 1 to 6. All worked out fine except the integer 6 where I need it to print out that the integer 6 is divisible by 2 and 3. How can I fix the error?
There are two problems here: first of all, the condition should be:
elif y %3 == 0 and y %2 ==0:
Since there are two separate conditions. Next you need to move that check to the top, so:
for y in x:
if y %3 == 0 and y %2 ==0:
print ("Divisible by 2 and 3!")
elif y %2 == 0:
print (y)
elif y %3 == 0:
print ("y")
else:
print ("Odd number!")
Why? Because if a number is dividable by 2 and 3, it is also dividable by 2. So that means in your case it would have picked the first branch. By swapping the order it will first check if it is dividable by 2 and 3. If that is not the case - that is if at least one condition fails - it will check the remaining branches.
The test for divisible by 2 and 3 should come first, so for 6 you have y %3==0 and y %2 ==0 evaluated instead of y %2 == 0:
for y in x:
if y % 3 == 0 and y % 2 == 0:
print ("Divisible by 2 and 3!")
elif y % 2 == 0:
print (y)
elif y % 3 == 0:
print (y)
else:
print ("Odd number!")

Python, TypeError: 'int' object is not iterable

Slight variation on FizzBuzz problem, just adding one more level.
I have read through the many answers to this Error message on SO, none of them have helped me grasp what I am missing.
So while I really appreciate the answers, what I want is to understand the WHY behind the error.
Can't this task be done using simple loop/if/elif structure, without calling a function?
TypeError: 'int' object is not iterable
def solution(N):
N = 0
for i in N:
if i % 3 == 0 and i % 5 == 0 and i % 7 == 0:
print('FizzBuzzWoof')
elif i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
else:
print(i)
N = N+1
def main():
solution(35)
main()
try this one:
def solution(ints):
for i in range(1, ints+1):
if i % 3 == 0 and i % 5 == 0 and i % 7 == 0:
print('FizzBuzzWoof')
elif i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0 and i % 5 == 0:
print('Fizz')
else:
print(i)
def main():
solution(35)
main()
put range(0, ints+1) if you want to start with 0 and include 35
You are trying to iterate through single number. Instead, you can create an array of ints and iterate through it. For instance:
ints = [1, 2, 3]
x.append([4, 5]) #append elements you need
print (x)
#result will be 1,2,3,4,5
And now go with this and iterate through 1 to 5:
for i in ints:
#your logic goes in here
In your code ints is a integer, 35 the provided example. Integers are not collections, thus you cannot use the as the range of a for-loop. As #AndrewLi suggests in his comment, use range(n) to get a iterator containing the elements 0 to n-1.
In your code you also overwrites ints by 0 as the fist thing in the solution function, effectively disregarding the argument parsed to the function. In the code below I have added range(arg), and renamed the argument to arg instead of ints.
def solution(arg):
ints = 0
for i in range(arg):
if i % 3 == 0 and i % 5 == 0 and i % 7 == 0:
print('FizzBuzzWoof')
elif i % 3 == 0 and i % 5 == 0:
print('FizzBuzz')
elif i % 3 == 0 and i % 5 == 0:
print('Fizz')
else:
print(i)
ints = ints+1
def main():
solution(35)
main()

Categories

Resources