Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help getting my program to reject a negative input from the user. Here is what I have: #short description of what the program

I need help getting my program to reject a negative input from the user.

Here is what I have:

#short description of what the program will do print("Hello, this program will ask you to enter a non-negative decimal integer. The program will then show the binary equivalent of the integer you choose.")

#ask the user to enter the non-negative decimal integer number= int(input("Enter a non-negative decimal integer: ")) while number< 0: print("Number Cannot Be Negative!! Try again...") #create a variable containing an empty character string output=' ' #create a copy of user's variable named "working" working = number #reject input less than zero

while True: #if working number is odd, prepend "1" to string variable if working%2==1: output="1"+output else: output="0"+output #if working number is even, preven "O" to string variable #print working number and character string print(str(working) + " " + output) #divide working number by 2, throwing away remainder working//=2 #stop when working number gets to zero if working==0: break #print user's original number and it's binary representation print("I think your original binary number " + str(number) + " is " +output+ " in binary.")

Thank you

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

More Books

Students also viewed these Databases questions