Can anyone help me with some Python coding? I'm trying to make a program that determines if something is a prime number and tries to find the next one.
print('Welcome to Prime Number Checker!')
num=int(input('Enter a number you would like to check: '))
for i in range(2,num):
if num%i==0:
print('Number is composite.')
break
else:
print('Number is prime and the next prime number is',end=' ')
num+=2
for i in range(2,num):
while num%i==0:
num+=1
i+=1
continue
print(str(num)+'.')
>>55795755
Who is that? Is she important?
For some reason, after "num+=2", it returns "35" as the prime number after 31.
I'm trying to divide a number by every number in a range and look for 0's in the remainder.
>>55795783
This code isn't making sense... Why do you have two loops I'm the second part? Why not have one , maybe two functions with one loop each. Why are you posting a homework question?
Sounds like homework to me, read the sticky faggot.
>>55795755
this is homework. fags pls go
>>55795883
It's not homework. It's just something I've been challenging myself to do.
>>55795904
...for a grade, in your very first """""CS""""" course
>>55795899
Delete this
>>55795899
/g/ - the StackOverflow for homework questions.
>>55795904
If this is challenging. Pick a new hobby like knitting or needle point m8
>>55795899
why did you delete your post?
>>55796042
probably because it's wrong anyway
>this entire thread
how can you fuck up so hard on a simple programming exercise
>>55795755
What the fuck are you doing?
>>55795781
That's moot, the founder of Microsoft.
>>55795755
1. use "[ code ]" and "[ / code ]" (without the blanks) for proper code fomratting. I can't even see if you used tabs here, which is crucial in python.
2. You don't have to check from 2 to "num". It's enough to check from 2 to "num/2". Think about it.
3. This line is wrong:print('Number is prime and the next prime number is',end=' ')
num+=2
You probably meant this:print('Number is prime and the next prime number is',end=' ')
num*=2
But this is also unnecessary, see next point:
4. The next lines are full of mistakes..
4a) If you use "for i in range(2,num)" you don't need "num+=1"! You're just pushing the upper bound further and further. Look up how loops work in python!
4b) The line "while num%i==0" is exatly the opposite of what you want, you want "while num%i != 0"
4c) You have way too many loops. Think about what you want to do: You need ONE "while True" statement, that's it. Then you just raise num in each iteration and check if it's a prime number.