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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:57:08+00:00 2026-06-12T00:57:08+00:00

I’m trying to control input to allow only greater than 0 numbers, but upon

  • 0

I’m trying to control input to allow only greater than 0 numbers, but upon testing this block of text, if I enter an illegal character first (a string, 0 or negative number), receive the error output and then input a valid value, it returns the first value I entered instead of the valid one just entered (which then causes the rest of my script to fail due to type mismatch or illogical values). I’ve tried moving the “return x” around but it does the same thing either way. says “variable x referenced before assignment” in the second case.

def getPrice():
    try:
        x = float(input("What is the before-tax price of the item?\n"))
        if x <= 0:
            print("Price cannot be less than or equal to zero.")
            getPrice()
        return x
    except ValueError:
        print("Price must be numeric.")
        getPrice()

and

def getPrice():
    try:
        x = float(input("What is the before-tax price of the item?\n"))
        if x <= 0:
            print("Price cannot be less than or equal to zero.")
            getPrice()
    except ValueError:
        print("Price must be numeric.")
        getPrice()
    return x

How can I fix this?

Also if you’re curious, this is for a school assignment, I’ve completed the entire program on my own but I just can’t figure out how to debug this.

Edit:

I got a working method now:

def getPrice():
    while True:
        try:
            x = float(input("What is the before-tax price of the item?\n"))
        except ValueError:
            print("Price must be numeric.")
            continue
        if x <= 0:
            print("Price cannot be less than or equal to zero.")
        else:
            return x
            break

and fixed the original code block (but it still uses recursion):

def getPrice():
        try:
            x = float(input("What is the before-tax price of the item?\n"))
            if x <= 0:
                print("Price cannot be less than or equal to zero.")
                x = getPrice()
        except ValueError:
            print("Price must be numeric.")
            x = getPrice()
        return x
  • 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-12T00:57:09+00:00Added an answer on June 12, 2026 at 12:57 am

    I’m not going to spell out an answer since it is homework, but I will point you in the right direction. First I suggest using a while loop instead of recursion.

    The code might look something like:

    while True:
        try:
            x = <get input>
            if input is good:
                break
            else:
                <print error>
        except badstuff:
            <Print error>
            next #redundant in this case as we'd do this anyways
    

    Second, the problem you’re having now has everything to do with variable scope. When bad input happens you call the function again, but do nothing with the output.

    Remember that the x in the first function call is completely separate from the x in the second function call.

    So if you intend to use recursion, you need to figure out how to pass the x value “back up the chain.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.