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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:56:08+00:00 2026-06-10T19:56:08+00:00

My lab asks me Prompt for the input of a number, accept either a

  • 0

My lab asks me “Prompt for the input of a number, accept either a positive or negative number.Use a dual alternative “decision” structure and print a message indicating whether the number entered is positive or negative.”

I did my code but for some reason its not working….

def getNumFromUser():
  num=input (“Please enter a number: “)
  if num >= 0:
      print "The number you entered  is positive"
  elif num  <= 0:
      print "The number you entered is negative"
  else:
     getNumFromUser() 

And it won’t run my code for some reason, when I take out elif statement it ask me to enter number and if I enter negative it will ask me to re-enter number to get positive …I just don’t know how to combine negative and positive number in code so it would “print out message indicating whether the number entered is positive or negative.” *I’m new to python programming so I’m lost here, I would appreciate if someone would explain to me*

  • 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-10T19:56:10+00:00Added an answer on June 10, 2026 at 7:56 pm

    The input function in python2.x will try and evaluate the string you pass it as code. This can be considered undesirable or dangerous, and it is usually recommended to use raw_input instead.

    That being said, raw_input will give you back a string. You will want to convert it to an int to compare to other ints:

    val_str = raw_input("enter a number")
    if int(val_str) >= 0:
        print "greater"
    

    Keep in mind that if the user does not enter a number, that example would crash. You can check that a string is a number by using: val_str.isdigit(). This works for ints, not floats. Part of your check can be to first confirm it is an int, otherwise ask again. Also, isdigit won’t properly detect a negative number, which means you might want to learn how to catch an exception that can get raised…

    As for your overall structure, I feel a simply while loop check would serve you better than a recursive call to getNumFromUser each time they enter bad information:

    def getNumFromUser():
    
        while True:
            num=raw_input("Please enter a number: ")
            try:
                num = int(num)
            except ValueError:
                # if the input cannot be converted into an int
                # then loop again
                continue
            else:
                # otherwise, we have an int, so stop looping
                break
    
        # num is now an int
        if num >= 0:
            print "The number you entered  is positive"
        else:
            print "The number you entered is negative"
    
        return num
    

    It may not be part of your assignment to expect the user to enter anything other than a valid int, but this example shows how to try and convert to int, and handle the failure.

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

Sidebar

Related Questions

Implementation Use: Data Structure Lab Exercise for October/28/2011 To do: Implement a Binary Search
My task in this lab is to accept multiple input file and the format
I'm using a serial terminal to provide input into our lab experiment. I found
i use lab.js 2.0.3 for parallel loading my scripts. the problem is, that in
In the lab I work in, we use Mozilla 1.7 for Sun Java™ Desktop
This is a lab assignment I am stuck on. I need to accept this
I'm using the Table of Report Lab library to print a table on a
Right now we are configuring a lab at the college to use the DarkGDK
My final lab task is cropping parts from an image, inserting the cropped image
Occasionally in our lab, our postgres 8.3 database will get orphaned from the pid

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.