0

Newbie in python and needs help for finding error in my code :')

v = [1] i = 2 n = 0 while True: x = int(input("Enter number : ") while i <= x: if x%i == 0: v.append(i) n += 1 i += 1 if n == 1: print(x + "has only 2 factors,hence it is a prime number") else: print("The factors of " + x + "are : "+ v) ----------------------------------------------------------------------------------------------- this code is to find the factors of an input, but I couldn't figure out which part of the code is wrong as invalid syntax occurs on the second (while) Could anybody help me out?

16th Sep 2020, 12:36 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
17 Answers
+ 9
in the line: x = int(input("Enter number : ") There is a closing parenthesis missing at the end of the line. After fixing this, i ended up in kind of infinite loop.
16th Sep 2020, 12:46 PM
Lothar
Lothar - avatar
+ 7
Just to find the factors of a number, a comprehension is a good way: inp = 16 res = [i for i in range(1, inp + 1) if inp % i == 0] # result in list res is: [1, 2, 4, 8, 16]
18th Sep 2020, 1:50 PM
Lothar
Lothar - avatar
+ 6
Bhavya Sarraf , your code is perfect, i would have suggested the same amendments. Well done👍
16th Sep 2020, 2:11 PM
Lothar
Lothar - avatar
+ 5
Shiau Huei Chang , i have put together some comments about your question on list comprehension. I have put it in a file, the text was to long to store it direct in the post area. https://code.sololearn.com/cXLH9e1170Dv/?ref=app
21st Sep 2020, 2:48 PM
Lothar
Lothar - avatar
+ 2
Nasif Rahman noted, i did it by accident, ald deleted the post xD
16th Sep 2020, 1:25 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 2
Thanks a lot Bhavya Sarraf ! I didnt knew the outer loop makes my loop infinite as i originally wanted to make it so that the user can insert input for infinity times, like how a calculator works, is there any way to do that?
17th Sep 2020, 6:17 AM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 2
Try this for factors n=int(input("enter the number")) m=1 While (m<=n) : if(n%m==0) : print(m) m+=1
18th Sep 2020, 11:31 AM
mannava daasaradhi
mannava daasaradhi - avatar
+ 2
Num = int(input('Enter the Number:') factors = [i for i in range(1,Num+1) if Num%i ==0] if len(factors) == 2: print(f'{Num} is Prime.') else: print(f'{Num} has factors {factors}')
19th Sep 2020, 5:52 PM
Karmakar Sunita
Karmakar Sunita - avatar
+ 2
Lothar brilliant! I could never know it could be written in this way as well! I understand the concept of it, but because I'm a bit new, i wanted to ask a few questions about your code xD Hope you don't mind 🙏🏻 First of all, what does the first ( i ) in the list mean though? Because uptill where I've learnt, i can understand (for i in range) , but not (i for i in range) haha xD Second, I thought ( if ) needs to come before the (i for i in range), or its because in this situation wr need to declare what is in the list first ? Anyways, great code from you, thanks a lot ✨
21st Sep 2020, 1:49 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 1
Lothar ah i see, true, after fixing it, the code didn't run its exact function( finding the factors) but only acted as an infinite loop of receiving inputs :'( Still couldn't figure out why tho
16th Sep 2020, 1:29 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 1
Bhavya Sarraf thanks a lot! ✨
17th Sep 2020, 10:53 AM
Shiau Huei Chang
Shiau Huei Chang - avatar
+ 1
Sandip Luhar i couldn't run your code though, because for me it said that i is not defined
21st Sep 2020, 2:23 PM
Shiau Huei Chang
Shiau Huei Chang - avatar
0
Thank you for your question. I also tried to make similar code. Please visit my code to evaluate. You can find my code after going to my profile. My code is public with name: num_factor_and_prime_num_while_loop_if_else_break_continue_python I hope comments will not distract in compiler section. #This code is to find... #1) How many modulo factors the number has. #2) If number is prime or not. while True: x = int(input("Enter natural number: ")) a=0 v=[] while True: a=x%i i=i-1 if a==0: v.append(i+1) if i==0: break else: continue print (v) if len(v)!=2: print ("It is not prime number") else: print ("Prime number")
18th Sep 2020, 6:26 PM
Sandip Luhar
Sandip Luhar - avatar
0
Karmakar Sunita nice! However, i havent learnt curly brackets yet, but ill reach there soon xD
21st Sep 2020, 1:59 PM
Shiau Huei Chang
Shiau Huei Chang - avatar