Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loop dilemma 2
#1
Hey guys.

So what I have today is another nested loop issue.
Program needs to ask a user for a number and save as variable (num)
I then need to determine if the number is bigger than 1, and if so:
determine if the number given by the user is a prime number or not.

Chances are fair that my approach is wrong but here is what I have come up with:
Trying to use a while loop as outer loop to start the inner for loop calculation
ONLY IF the number given by the user is greater than one.

at first I only had the for loop but I get a false positive if the user enters the number 1.

It seems to work apart from the fact that its now an infinite loop, and moving the break command back seems
to kill the calculation.

Any advice would be very much appreciated.
Here is what I have:

num = int(input("Enter a number here: "))

while num >= 2:
    for i in range(2, num):
        if num % i  == 0:
            print(num," is not a prime number.")
            break
    else:
        print(num," is  a prime number.")
Reply


Messages In This Thread
nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 08:55 AM
RE: nested for loop dilemma 2 - by luoheng - Sep-12-2019, 09:18 AM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 09:47 AM
RE: nested for loop dilemma 2 - by perfringo - Sep-12-2019, 09:29 AM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 11:13 AM
RE: nested for loop dilemma 2 - by ichabod801 - Sep-12-2019, 11:40 AM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 11:50 AM
RE: nested for loop dilemma 2 - by perfringo - Sep-12-2019, 12:01 PM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 12:10 PM
RE: nested for loop dilemma 2 - by perfringo - Sep-12-2019, 12:21 PM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 12:41 PM
RE: nested for loop dilemma 2 - by perfringo - Sep-12-2019, 01:04 PM
RE: nested for loop dilemma 2 - by YoungGrassHopper - Sep-12-2019, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,396 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,603 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,574 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,240 Nov-25-2020, 10:04 AM
Last Post: palladium
  dilemma with list comprehension spalisetty06 1 1,933 Aug-14-2020, 09:18 AM
Last Post: buran
  Nested loop indexing Morte 4 3,936 Aug-04-2020, 07:24 AM
Last Post: Morte
  Nested for loop not looping puttingwordstogether 0 1,718 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,869 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Nested Loop for user input Ads 2 3,566 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 5,699 Oct-16-2019, 03:11 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020