Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:50:14+00:00 2026-06-18T14:50:14+00:00

I am currently writing a program in Python 3.3.0 which prints out the n

  • 0

I am currently writing a program in Python 3.3.0 which prints out the n first square numbers and finally prints out their sum. The condition is that the user can only compute an integral number of terms greater than zero. Here is the code:

print("WELCOME!")

n = input("How many numbers to sum up?: ")

while n <= 0:
    print("You have to write a positive integer; try again!")
    n = input("How many numbers to sum up?: ")

i = 1
sum = 0
while 0 < i <= n:
    print(i,"*",i,"=", i**2)
    sum += i**2
    i += 1
print("Sum:", sum)

Now, I managed to code the error message for when the user types in a negative number. However, I am having difficulties coding an error message for when the user types in a string, such as “fifteen”.

I want the program to allow the user to try assigning ‘n’ a value once again and again, just as if they had typed a negative number; basically I want to code a loop asking for the same thing over and over again if the user inputs a string.

The problem is that input() always assigns a string to an arbitrary variable, so I tried writing code for converting the string to an integer. It works fine when the user inputs an integral value, however it cannot define int(n) if ‘n’ is not an integer.

I have googled a lot regarding this issue and I found some examples using try and except ValueError but none of them seem to be able to create a loop out of it.

Anybody got an idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:50:15+00:00Added an answer on June 18, 2026 at 2:50 pm

    Put the try/except inside the loop.

    while True:
        try:
            n = int(input("How many numbers to sum up?: "))
            if n <= 0:
                print("You have to write a positive integer; try again!")
                continue
            break
        except ValueError:
            print("You have to write a positive integer; try again!")
    

    You could also replace the last part of your code (the part after the first loop) with this:

    squares = lambda x: [print('{0} * {0} = {1}'.format(x, x**2)), x**2][1]
    
    print("Sum:", sum(squares(i) for i in range(1, n+1))
    

    The lambda isn’t recommended though ;). You could of course use a normal function instead:

    def squares(x):
        print('{0} * {0} = {1}'.format(x, x**2))
        return x**2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Python program which runs a virtual terminal. Currently I am
I am currently writing a program that has a function that prints a linked
I'm currently writing a program in Python, and I would like to determine the
I am currently writing a program using Weka that builds a model (using one
I'm currently writing a java program that requires some Data to run. The data
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I am currently writing an app in python that needs to generate large amount
I am writing a program in Python 2.7 that retrieves remote files and dumps
I am currently writing a program which is having a lot of code duplication
I am currently writing a program that has a function that needs to delete

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.