Programming

Programming Questions? Ask a Programmer for Answers ASAP

Ask a Computer Expert,
Get an Answer ASAP!

Programming
This answer was rated:

How can I get this python script to only accept numbers 0-10

for rating input? def main():...
How can I get this python script to only accept numbers 0-10 for rating input?



def main():
name=raw_input('Enter the name: ')
while name =='':
print'Error in input. Name cannot be blank.'
name=raw_input('Try again. Enter the name: ')

rating=raw_input('Enter the evilness rating: ')

while rating.isdigit == False:
print'Error in input.Rating must be a non-negative integer.'
rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)
laugh=' Bwa'+'ha'*int(rating)
print 'My name is %s.%s.'%(name,laugh)

main()
Show More
Show Less
Ask Your Own Programming Question
Answered in 2 minutes by:
8/4/2011
ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 9,151
Experience: MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
Verified

ATLPROG :

Hi Welcome to Just Answer, It will be my pleasure to help you.

Customer:

Hi

ATLPROG :

Hello

ATLPROG :

What version of Python are you using ?

ATLPROG :

2.6 ?

Customer:

2.7

ATLPROG :

k

ATLPROG :

and can you please help me understand what are you writing this program for ?

Customer:

homework

ATLPROG :

I mean can you give me your assignment or homework details

Customer:

It's quite lengthy.I need to write a program that accepts a name but will not accept a blank input for name.It needs to accept a user input rating from 0-10. Then it either returns a statement that multiplies the laugh# XXXXX the rating number or an error message. It all works except the rating must be limited to 0 to 10 inclusive.

ATLPROG :

ok...

ATLPROG :

Pythin code on the question text is not formatted well

ATLPROG :

can you send me your .py file ?

ATLPROG :

Can you please go to www.wikisend.com and upload your file ? Once you do that you will get a page that has the "File ID" and "Download Link". Copy the "Download Link" or "File ID" and please paste it in your reply here.

Customer:

Can't do that. I have to go to work. Thought this might be an easy way to get a simple answer to my homework question.

ATLPROG :

yes, but your code is not formatted very well...and python is very much space sensitive

ATLPROG :

Let me give it a shot regardless then, if you don't want to upload code

ATLPROG :

try this ...

ATLPROG :

rating=int(raw_input('Enter the evilness rating: '))

ATLPROG :

that should work

ATLPROG :

let me know what you find

Customer:

That will not help at all. I need to limit the numbers that can be input to 1 -10

ATLPROG :

ok...

ATLPROG :

didn't realise you need to do that

ATLPROG :

CAn I write code to prompt user to re-enter if its a invalid number ?

Customer:

If it's an invalid number it should say "Sorry that rating is invalid"

ATLPROG :

ok..

ATLPROG :

working on adding that code

Customer:

My teacher suggests a loop sentinel, but not necessary.

ATLPROG :

k

ATLPROG :

I have this ready

ATLPROG :

let me send you a link

ATLPROG :

Please download the program from this Answer_Link

ATLPROG :

Let me know if you still have questions

ATLPROG :

There is a RAW Paste data in section below the formatted section on link I gave you, which should help

Customer:

There is a link answerlink but I can't find any information

ATLPROG :

can you click on that link please ?

ATLPROG :

WHat do you see when you open that link ?

ATLPROG :

Let em paste it here in this chat

ATLPROG :


def main():

name=raw_input('Enter the name: ')

while name =='':

print'Error in input. Name cannot be blank.'

name=raw_input('Try again. Enter the name: ')

rating=raw_input('Enter the evilness rating: ')

while rating.isdigit == False:

print'Error in input.Rating must be a non-negative integer.'

rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)


while rating > 10 or rating < 1:

print'Sorry that rating is invalid.'

rating=raw_input('Try again. Enter the evilness rating: ')

rating =int(rating)


laugh=' Bwa'+'ha'*int(rating)

print 'My name is %s.%s.'%(name,laugh)

main()

ATLPROG :

there you go

ATLPROG :

Did it help ?

ATLPROG :

???

ATLPROG :

you still with me ?

Customer:

yes, but I can't seem to figure out the formatting.

Customer:

When I click on the link if something comes up it;s all java

ATLPROG :

well thats why I pasted you the code above

ATLPROG :

Did that work ?

Customer:

No because I can't figure out the indenting.

ATLPROG :

Well now you know why I was asking you for a python file my friend !

ATLPROG :

Anyways, Try this LINK

ATLPROG :

Python code should be there

ATLPROG :

let me know if you still have issues

ATLPROG :

Did it work ???

Customer:

Sorry but it totally doesn't work now. I f you run the program it gives me an error message for any rating number now. I do know that the rating changes must be done within the same while loop to work. Like I said my teacher suggests a loop sentinel.

ATLPROG :

hmm...are you sure you are running my program ?

Customer:

Yes. Copy and paste.

ATLPROG :

Let me give you a different link

ATLPROG :

Try this LINK

ATLPROG :

Wait a second, I see the problem...why it works some times and why it does not

Customer:

I realize that programmers don't usually use idle, but there must be some way to try out the script.If you enter Bob for the name and 5 for the rating. It should print My name isXXXXX Insead I recieve the error message using your script.

ATLPROG :

ok...Its because of different version of Python...

ATLPROG :

This should work with Python 2.7

ATLPROG :

Try this LINK

Customer:

Okay, it works great now, BUT it still does not respond correctly if you enter alpha characters into the rating. It should only accept numbers 0 through 10 or return an error message. That's why I believe it cannot be accomplished with 2 separate while statements.

ATLPROG :

let me look

ATLPROG :

Try this LINK

Customer:

Can you tell me what you did. I don't see the difference. My teacher said 2 separate while loops would not work but it does. And if it works I get the credit. This was the only assignment I couldn't figure out this quarter. thanks.

ATLPROG :

any luck ?

ATLPROG :

I am nto sure why your teacher would say two separate while loops won't work

Customer:

It's good, bit can you explain the last change?

ATLPROG :

I don't have all your 'requirement's or task details, but from what you ahve told me, it works

ATLPROG :

yes I can

ATLPROG :

I am using rating.isdigit() method to find out if its a digit

ATLPROG :

and this method works only on strings

ATLPROG :

not on ints

ATLPROG :

so the conversion to integer should happen after this check is complete

ATLPROG :

We can do this with one while loop too , if thats what you would like

ATLPROG :

but the messages may get tricky to handle

Customer:

No, it's good.

ATLPROG :

cool

Customer:

Thanks we are done.

ATLPROG :

I hope you get good grade

ATLPROG :

Please start your question with "For ATLProg only" if you would need any help on Programming related questions in future.

ATLPROG :

Please remember to hit the Green accept button if you think my answer helped, so that I get paid for the time spent. I would obviously continue to help you if you have any further questions.

Customer:

You may be hearing from me.

ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 9,151
Experience: MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
Verified
ATLPROG and 87 other Programming Specialists are ready to help you
Ask your own question now
Customer reply replied 6 years ago
After thoroughly trying the script, it still doesn't work.If you make a couple incorrect answers first-such as,15 or dog in the rating, it will then return with the correct input such as 6 as incorrect. So it STILL does not work. Can you fix it?
Tamera
SO what are the inputs that it does not work for ? Can you please provide them more clearly ?

Can you send me the data values you are testing it with. I will take a look
Ask Your Own Programming Question
Customer reply replied 6 years ago
Here's the output of the script.It seems to work just fine and then it crashes,at about the third input.
Is that enough information?
Tamera

Enter the name: Bobby
Enter the evilness rating: 46
Sorry, rating must be between 0 and 10, inclusive.
Try again. Enter the evilness rating: -15
Sorry, rating must be between 0 and 10, inclusive.
Try again. Enter the evilness rating: cat

Traceback (most recent call last):
File "C:\Users\Tamera\Desktop\lab7_input_validation.py", line 29, in <module>
main()
File "C:\Users\Tamera\Desktop\lab7_input_validation.py", line 24, in main
rating =int(rating)
ValueError: invalid literal for int() with base 10: 'cat'
>>>
Please download the program from this Answer_Link
Ask Your Own Programming Question
Ask ATLPROG Your Own Question
ATLPROG
ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 9,151
9,151 Satisfied Customers
Experience: MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML

ATLPROG is online now

A new question is answered every 9 seconds

How JustAnswer works:

  • Ask an ExpertExperts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional AnswerVia email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction GuaranteeRate the answer you receive.

JustAnswer in the News:

Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.

What Customers are Saying:

My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed.

One Happy CustomerNew York

Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help.

Mary C.Freshfield, Liverpool, UK

This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!!

AlexLos Angeles, CA

Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult.

GPHesperia, CA

I couldn't be more satisfied! This is the site I will always come to when I need a second opinion.

JustinKernersville, NC

Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around.

EstherWoodstock, NY

Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know.

RobinElkton, Maryland

< Previous | Next >

Meet the Experts:

ATLPROG

ATLPROG

Computer Software Engineer

9,151 satisfied customers

MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML

LogicPro

LogicPro

Computer Software Engineer

7,131 satisfied customers

Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.

unvadim

unvadim

Computer Software Engineer

1,168 satisfied customers

Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.

lifesaver

lifesaver

Computer Software Engineer

950 satisfied customers

Several years of intensive programming and application development experience in various platforms.

ehabtutor

ehabtutor

Computer Software Engineer

936 satisfied customers

Bachelor of computer science, 5+ years experience in software development, software company owner

TheDoctor

TheDoctor

Software Engineer

893 satisfied customers

M.S. in Internet Information Systems

The-PC-Guy

The-PC-Guy

Software Engineer

800 satisfied customers

Extensive Knowledge in PHP, MYSQL, CSS & Javascript

< Previous | Next >

Related Programming Questions
How do I get my restored Microsoft Wordpad .rtf files to
How do I get my restored Microsoft Wordpad .rtf files to stop coming up in Landscape orientation. I want them to normally come up in Portrait orientation? (Tech Viet helped me a few days ago.)… read more
David L.
David L.
Bachelor\u0027s Degree
6 satisfied customers
I am looking for help understanding the content of some
Hi. I am looking for help understanding the content of some files. Ideally, I could have someone explain what certain things mean so that I am not making assumptions. The files are in HTML, JS, SQL, a… read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
Searching for a tutorial on WordPress theme ColorMag Pro -
Searching for a tutorial on WordPress theme ColorMag Pro - some I have found have no sound and or the narration isn't understandable. … read more
RussellA
RussellA
Solutions Architect
Bachelor\u0027s Degree
121 satisfied customers
VWorker01.pyw import sys; print (sys.version) n = 4; m = 2 M
vWorker01.pyw import sys; print (sys.version) n = 4; m = 2 M = n * [n * [0.0]] print("show M contents: ") print (M) for i in iter(range(m,n)): M[i][i] = 1.0 print ("show M contents: ") print(M)# For c… read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
I must have messed up something on my
I must have messed up something on my oshaukutaoutdoors.wordpress.com site as I can't get into it. I am hoping you can help me out … read more
RussellA
RussellA
Solutions Architect
Bachelor\u0027s Degree
121 satisfied customers
Is there a way I can get Microsoft excel to stop using the
is there a way I can get Microsoft excel to stop using the scientific notation and altering my data? for example, some numbers shows up as 75645+0545. I need this to stop and to get it to stop saving … read more
The-PC-Guy
The-PC-Guy
Software Engineer
Bachelor's Degree
800 satisfied customers
I want to remove search items from my gmail search list the
i want to remove search items from my gmail search list the case is .... when i search something in gmail it shows me a list o messages, but i already deleted these messages.. now i don't want that th… read more
Pete
Pete
Engineer
Bachelor\u0027s Degree
1,123 satisfied customers
I need the source code or the library in Java that defines
I need the source code or the library in Java that defines OUAnimatedApplication and OUColour … read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
I work in computer/network security. In order to really
I work in computer/network security. In order to really become an expert in that field, I need to have an intimate understanding of the C programming language. This helps to do tasks like code auditin… read more
Martin
Martin
Network Engineering Diploma
141 satisfied customers
CGI/PERL I need help with a project that has the guidelines
CGI/PERL I need help with a project that has the guidelines listed. if it can be done within a week , I will be overly... appreciative.… read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
My excel files are locked for editing I am the only user.
my excel files are locked for editing I am the only user … read more
Kamil Anwar
Kamil Anwar
IT Consultant / Systems Engineer
Bachelor\u0027s Degree
433 satisfied customers
I have a financial table using positive and negative
I have a financial table using positive and negative numbers, randomly. I have an equations two find the balance, but Excel is not using the signage of the numbers. … read more
Kris R
Kris R
IT Manager
Computing
2,596 satisfied customers
Not able to use Word and Excel. Have not since Windows 10
Have not since Windows 10 was installed in Jan. 2016 … read more
Jason Jones
Jason Jones
AAS Information Technology
6,320 satisfied customers
I have mapped a domain to my wordpress site and I wish to
Hi pearl - i have mapped a domain to my wordpress site and I wish to change it - do I need to pay for this to happen? … read more
Kamil Anwar
Kamil Anwar
IT Consultant / Systems Engineer
Bachelor\u0027s Degree
433 satisfied customers
Copy & paste cell comments from one worksheet to another
Copy & paste cell comments from one worksheet to another with vba excel. Please find attached detailed reference workbook. Solution with vba excel only.… read more
Ingo U
Ingo U
Master\u0027s Degree
357 satisfied customers
Looking for some help writing this program. Its perl/python
Looking for some help writing this program. Its perl/python scripting. Write a Python program that prints "DU" , similar to the image i have attached. I am looking for the Python code (.py) file and t… read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
I have an excel question. 3 days ago. Utilizing a
utilizing a spreadsheet I've built. Attempting to find a solution to a formula problem. … read more
George Sibiya
George Sibiya
PhD
216 satisfied customers
I need help with an If formula: English. I would like to
I would like to create a formula that: IF a cell is =too or greater than 100% than the outcome would be $25, if that same cell is greater than 125% than the outcome would be $35 … read more
The-PC-Guy
The-PC-Guy
Software Engineer
Bachelor's Degree
800 satisfied customers
Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.

Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.

Show MoreShow Less

Ask Your Question

x