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 8807363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:21:04+00:00 2026-06-14T02:21:04+00:00

my program takes a mathematical input and checks it for errors before proceeding, here

  • 0

my program takes a mathematical input and checks it for errors before proceeding, here is the part of the code I need help with:

expression= introduction()#just asks user to input a math expression    
operators= set("*/+-")
numbers= set("0123456789")
for i in expression:
    while i not in numbers and i not in operators:
        print("Please enter valid inputs, please try again.")
        expression= introduction()

Now I have set up an error loop but the problem I am having is that I don’t know what to update the loop with in this scenario. Anyone?

I need something simple. I need something close to the code that is posted in this OP. Something like:

expression= introduction()#just asks user to input a math expression    
operators= set("*/+-")
numbers= set("0123456789")
while True:
    for i in expression:
        if i not in numbers and i not in operators:
            print("Please enter valid inputs, please try again.")
            expression= introduction()
    else:
        break

Note that this code doesn’t work either. It loops for every single mistake that the user inputs for “expression”.

Things such as what is below are too advanced and I cannot use them:

valid = operators | numbers
while True:
    expression = introduction()
    if set(expression) - valid:
        print 'not a valid expression, try again'
    else: 
        break

import string

expression = introduction()    
operators = {'*', '/', '+', '-'}
numbers = set(string.digits)
while any(char not in numbers and char not in operators for char in expression):
    print("Please enter valid inputs, please try again.")
    expression = introduction()
  • 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-14T02:21:05+00:00Added an answer on June 14, 2026 at 2:21 am

    You were very close in your 2nd code. You would need to do some changes, and your code should be like this: –

    expression = ""
    operators= set("*/+-")
    numbers= set("0123456789")
    while True:
        expression= introduction()  # You should read the next expression here
        for i in expression:
            if i not in numbers and i not in operators:
                print("Please enter valid inputs, please try again.")
                break # You should have a break here
        else:
            break
    
    print expression  # Print if valid
    
    • If the expression is not valid, it will only break out of for loop
      and continue the while loop.
    • And, if the expression is valid, it will execute the else block of your for-loop and
      break out of the while loop.
    • To use your expression outside your while loop, you would need to declare it outside the while loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my program takes a mathematical input and checks it for errors before proceeding, here
What is this called, when a program takes source code as an input and
My program takes user name and password authentication from user before initialising the program,
I wrote this function to communicate with an external program. Such program takes input
I have a perl program that takes input and output file arguments, and I'd
This program takes two input . first Array size(n) and choice ex:- if n
So here I am trying to debug this issue where the program takes forever
This program takes 2 numbers from user input, asks them whether they'd like to
I've written some code to implement an algorithm that takes as input a vector
I have a mathematical formula in my program that takes in two values, both

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.