Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write a program that repeatedly asks a user to enter a positive integer until they enter a positive integer number

I’m trying to write a program that uses a while loop to repeatedly ask a user to input a positive integer until they do but my code keeps printing the number after the while look asks a second time and I don’t want that. This is my code below:

num = int(input("Enter a positive integer: "))

while num <= 0:
    print(int(input("Enter a positive integer: ")))

print(f"{num} is positive.")

this is what prints when I execute the code:

Enter a positive integer: -4

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Enter a positive integer: -3

-3

Enter a positive integer: -6

-6

Enter a positive integer:

>Solution :

num = int(input("Enter a positive integer: "))

while num <= 0:
    num = int(input("Enter a positive integer: "))

print(f"{num} is positive.")

This should work. Your while-loop is checking if the number is positive, but you are not changing the number based on the new input.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading